以下代码:
function A() {
this.method_this_outsideReturn = function() {
console.log('method_this_outsideReturn')
};
return {
method_this_insideReturn: function() {
console.log('method_this_insideReturn')
},
};
}
var a = new A();
console.log(a.method_this_insideReturn()); // This would work.
console.log(a.method_this_outsideReturn()); // This wouldn't work. Warns attri_this_outsideReturn undefined
但是,在注释掉返回后:
function A() {
this.method_this_outsideReturn = function() {
console.log('method_this_outsideReturn')
};
/*return {
method_this_insideReturn: function() {
console.log('method_this_insideReturn')
},
};*/
}
console.log(a.method_this_outsideReturn()); // This would work now
为什么会这样?return 在构造函数中做了什么?当 return 语句不存在时会发生什么?