我正在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)
- 1 回答
- 0 關注
- 144 瀏覽
添加回答
舉報
0/150
提交
取消