“这是怎么回事?”在构造函数中赋值的函数中工作
How does "this" work in functions that are assigned in the constructor?
我找到了这个示例代码:
function personFullName() {
return this.first + ' ' + this.last;
}
function Person(first, last) {
this.first = first;
this.last = last;
this.fullName = personFullName;
}
var dude = new Person("Michael", "Jackson");
alert(dude.fullName());
提示"Michael Jackson"。我将其更改为从构造函数中调用personFullName
,而不是分配函数对象:
function personFullName() {
return this.first + ' ' + this.last;
}
function Person(first, last) {
this.first = first;
this.last = last;
this.fullName = personFullName();
}
var dude = new Person("Michael", "Jackson");
alert(dude.fullName);
我希望"fullName"属性现在是一个字符串而不是一个函数。但现在它警告"未定义未定义"。谁能解释一下为什么我的版本不行?
在JavaScript中,this
通常是函数调用中.
之前的任何内容。因此,您说的dude.fullName()
是导致fullName()
中的this
被设置为dude
1的原因。
在你问题的第二个版本中,你没有用同样的方式来称呼它。您调用personFullName()
时前面没有任何内容(这是正确的,因为它不再附加到Person对象)。这意味着this
最终默认为与window
相同的值。由于window
没有设置first
或last
属性,因此this.first
和this.last
都是undefined
。
要解决这个问题,您可以将您的person作为personFullName()函数的参数:
function personFullName(person) {
return person.first + ' ' + person.last;
}
然后命名为
…
this.fullName = personFullName(this);
1:注意,该方法必须是对象的属性,才能使this
绑定工作。您不能仅仅调用object.someMethod()
,并在someMethod
中将this
设置为object
。在您的代码中,以下内容将无法工作:
function Person(first, last) {
this.first = first;
this.last = last;
this.fullName = this.personFullName();
}
Uncaught TypeError: this.personFullName is not a function
也不会:
function personFullName() {
return this.first + ' ' + this.last;
}
function Person(first, last) {
this.first = first;
this.last = last;
}
var dude = new Person("Michael", "Jackson");
alert(dude.personFullName());
Uncaught TypeError: dude.personFullName is not a function
您可以在任何情况下使用apply
helper方法来绕过此限制:this.fullName = personFullName.apply(this)
执行您期望的第二版本代码所做的工作,并且您还可以在任何时候调用personFullName.apply(dude)
并获得"Michael Jackson"
。
this
是personFullName
函数中的窗口,因为它没有在正确的上下文中调用。您可以使用apply在正确的上下文中调用它,而无需修改personFullName
函数。
function personFullName() {
return this.first + ' ' + this.last;
}
function Person(first, last) {
this.first = first;
this.last = last;
this.fullName = personFullName.apply(this); // The magic
}
var dude = new Person("Michael", "Jackson");
alert(dude.fullName);
一个更好的解决方案是:
Person.prototype.personFullName = function() {
return this.first + ' ' + this.last;
}
在第二个示例中访问this
的上下文引用window
对象。window
没有设置fullName
属性。如果您将alert(this);
添加到这两个函数中,您就会明白我的意思。
- Javascript:应为赋值或函数调用,但实际看到的却是表达式
- 赋值后的回调函数
- 可以使用属性赋值实现多个函数
- 将ECMAScript 6析构函数赋值(ES2015)重构为旧版本的javascript
- Javascript:JSHint:应为赋值或函数调用,但实际看到的却是表达式
- 我可以在javascript中的回调函数中为变量赋值吗
- 错误-应为赋值或函数调用,但实际看到的却是表达式
- 将for循环的值赋给函数d3
- 内部函数不会为外部函数在 jQuery 中动态创建的元素赋值
- “期望赋值或函数调用,而是看到了表达式.” - 不必要的控制台.log
- Js提示:“;期望一个赋值或函数调用,而看到一个表达式“;,Switch语句
- Javascript值未定义,函数在赋值后运行
- 如何在另一个java脚本函数中访问java脚本中动态html表中单元格的动态赋值
- 将函数赋值给 javascript 中的元素
- 如何将函数返回的值赋值到变量中
- 在 JavaScript 中,未声明的变量赋值可以在函数范围之外访问
- 如何获取嵌套在两个赋值函数和一个 forEach 循环中的值
- 在JS中为变量赋值函数时,为什么会自动执行
- 在JavaScript中调用赋值函数
- 用循环赋值函数实参