4 回答

TA貢獻1803條經驗 獲得超6個贊
function A() {
this.Avar = 'hello'
}
A.prototype.breadcrumb = function() {
const ctx = this
return {
push: function() {
console.log(ctx.Avar)
}
}
}
new A().breadcrumb().push()

TA貢獻1719條經驗 獲得超6個贊
在不依賴實例的情況下是有方法的。
不過不確定你的需求是什么,如果僅僅是個鏈式操作可以這樣。
"use strict";
function A(){
this.Avar = 'hello';
}
A.prototype.breadcrumb = function() {
return {
push: () => {
console.log(this.Avar);
}
};
};
new A().breadcrumb().push(); // 'hello'

TA貢獻1836條經驗 獲得超3個贊
function A(){
this.Avar = 'hello';
var self = this;
this.breadcrumb = {
push:function(){
console.log(self.Avar);
}
};
}
var aa = new A();
aa.breadcrumb.push();
添加回答
舉報