Javascript:无法访问显式设置的对象变量(不在构造函数中)

时间:2019-07-12 06:12:54

标签: javascript

const Person = function(name){
    this.name= name;
    this.getName = () => {
        return this.name;
    }
}

let person = new Person('Mark');

person.city = 'New York';
Person.prototype.getCity = () => {
    return this.city;
};

console.log(person.getName());
console.log(person.getCity());

输出:

标记 未定义


为什么this.city返回undefined?有什么解释吗?

0 个答案:

没有答案