使用equal运算符的Javascript函数和对象
我试图从中理解Javascript概念。 请参阅下面的代码使用equal运算符的Javascript函数和对象,javascript,function,object,equals,operator-keyword,Javascript,Function,Object,Equals,Operator Keyword,我试图从中理解Javascript概念。 请参阅下面的代码 function personFullName() { return this.first + ' ' + this.last; } function personFullNameReversed() { return this.last + ', ' + this.first; } function Person(first, last) { this.first = first; this.last = las
function personFullName() {
return this.first + ' ' + this.last;
}
function personFullNameReversed() {
return this.last + ', ' + this.first;
}
function Person(first, last) {
this.first = first;
this.last = last;
this.fullName = personFullName;
this.fullNameReversed = personFullNameReversed;
}
我不明白为什么函数personFullName()的调用方式是
this.fullName = personFullName;
为什么不叫喜欢
this.fullName = personFullName();
下表相同
this.fullNameReversed = personFullNameReversed;
我知道函数是javascript中的对象,但我无法理解这个概念?因为
Person
对象将自己指定为方法,而不是函数的结果。这就是它不调用函数的原因
这样你就可以做到
var p = new Person("Matt", "M");
p.fullName(); // Returns "Matt M"
p.fullNameReversed(); // Returns "M, Matt"