【发布时间】:2017-11-29 18:43:37
【问题描述】:
var userData = {
id: 2,
name: 'Tim'
}
function Userdata( id, name) {
this.id = id;
this.name = name;
this.getData = () => {
console.log('Name: ' + this.name + ' and Id ' + this.id )
}
}
var ud = new Userdata(1, 'Tom');
ud.getData.call(userData);
输出:姓名:Tom 和 Id 1(为什么)
虽然 ud.getData.call(userData) 会在调用函数时将 this 设置为 userData,但这并没有发生。
【问题讨论】:
标签: javascript function.prototype