function importantPerson () {
console.log (this.name)
}
const name = 'Sunny';
const obj1 = {
name : 'Cassy',
importantPerson : importantPerson
}
有人可以解释这段代码的含义吗?importantPerson : importantPerson
认为该属性是实际功能的地址持有者。
importantPerson (property holding pointer to function) : importantPerson(function)
所以当您这样做
obj1.importanPerson()
调用importantPerson
的obj1
键将执行该功能。
有一个对象obj1
,其键为[“ importantPerson”],其值为函数importantPerson
function importantPerson () {
console.log (this.name)
}
const name = 'Sunny';
const obj1 = {
name : 'Cassy',
importantPerson : importantPerson
}
obj1.importantPerson()
[@Nick
评论:
importantPerson是对象中的一个属性,它指向您的personalPerson函数的参考]
尝试以下示例以使其更清晰:
function importantPerson() {
return this.name;
}
const name = 'Sunny';
const obj1 = {
name: 'Cassy',
importantPerson: importantPerson
}
const obj2 = {
name: 'name2',
importantPerson: importantPerson
}
console.log(obj1.importantPerson());
console.log(obj2.importantPerson());
importantPerson是一个引用ImportantPerson函数的键。所以当你做这个:
obj1.importantPerson
您正在调用一个重要的人函数