function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}
var MyFather = new Person('John', 'Doe', 22, "blue");
function getName() {
let name = this.firstName + ' ' + this.lastName;
return name;
}
MyFather.getName = getName;
//Will Return John Doe
console.log(MyFather.getName());
var getAge = () => {
let age = this.age;
return age;
}
MyFather.getAge = getAge;
//will return undefined
console.log(MyFather.getAge())
在上面的代码中,函数get-name将返回所需的结果。但是,当我使用arrorw函数获取getAge时,它将返回undefined。
我知道箭头功能中的关键字this表示窗口对象。反正还有年龄吗?使用此
尝试一下。
var getAge = () => {
let age = this.age;
return age;
}
MyFather.getAge = getAge.bind(MyFather);
//will return undefined
console.log(MyFather.getAge())