亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在函數中使用 this 關鍵字設置屬性

在函數中使用 this 關鍵字設置屬性

開滿天機 2023-10-24 17:39:36
我正在makerjs練習javascript. 當我使用時,我遇到了關鍵字問題this。//render a model created by a function, using the 'this' keywordvar makerjs = require('makerjs');function myModel() { var line = {    type: 'line',    origin: [0, 0],    end: [50, 50]   }; var circle = {    type: 'circle',    origin: [0, 0],   radius: 50  }; var pathObject = { myLine: line, myCircle: circle };//set properties using the "this" keyword ***here I dont' understand this.paths = pathObject;}//note we are using the "new" operatorvar svg = makerjs.exporter.toSVG(new myModel());document.write(svg);我不明白這段代碼是如何工作的。使用此關鍵字保存后,如下所示, this.paths = pathObject;如果不返回任何東西,這怎么行?
查看完整描述

1 回答

?
PIPIONE

TA貢獻1829條經驗 獲得超9個贊

您的myModel函數不一定需要返回任何內容,它還可以通過this.?makerjs.exporter.toSVG正在尋找您在下面一行中公開的實例的paths屬性。new myModel()

this.paths?=?pathObject;

在上面的行中,您正在paths當前實例上創建一個屬性,其中當前實例是通過 訪問的this。正如您在下面的代碼片段中看到的,我可以paths使用m.paths.


function myModel() {


?var line = {?

? ?type: 'line',?

? ?origin: [0, 0],?

? ?end: [50, 50]?

? };


?var circle = {?

? ?type: 'circle',?

? ?origin: [0, 0],

? ?radius: 50

? };


?var pathObject = { myLine: line, myCircle: circle };


//set properties using the "this" keyword ***here I dont' understand

?this.paths = pathObject;

}


let m = new myModel();


console.log(m.paths)


查看完整回答
反對 回復 2023-10-24
  • 1 回答
  • 0 關注
  • 144 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號