javascript 为什么greet函数没有返回期望值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17713630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does the greet function not return the expected value?
提问by flylib
Question:
问题:
Why does the greet function not return the expected value?
为什么greet函数没有返回期望值?
Code:
代码:
function Person(name){
this.name = name;
}
Person.prototype.greet = function(otherName){
return "Hi" + otherName + ", my name is " + name;
}
How do I answer this? I create a new person then what do I do?
我该如何回答?我创建了一个新人,然后我该怎么办?
var John = new Person("John");
回答by Matthew Graves
Wrong access method. the variable name
isn't defined, only this.name
is defined. So it's looking for a variable in the function scope called name
instead of a property of the object called name
.
错误的访问方法。变量name
没有定义,只有this.name
定义了。所以它在被调用的函数作用域中寻找一个变量,name
而不是被调用对象的属性name
。
To access an object's property from within the object we use the this
keyword. Thus we'll need to use this.name
to access the name
property in the implementation below.
要从对象内部访问对象的属性,我们使用this
关键字。因此,我们需要使用this.name
来访问name
下面实现中的属性。
Person.prototype.greet = function(otherName){
return "Hi" + otherName + ", my name is " + this.name;
}
回答by RobG
In your code:
在您的代码中:
> function Person(name) {
> this.name = name;
> }
When called as a constructor, the above will create a named property of an instance called nameand assign it the value of the nameparameter.
当作为构造函数调用时,上面将创建一个名为name的实例的命名属性,并为其分配name参数的值。
> Person.prototype.greet = function(otherName){
> return "Hi" + otherName + ", my name is " + name;
> }
Here the identifier nameis used as a variable, but the identifier you are looking for is a named property of the instance, so you need to access it at as such. Typically, this function will be called as a method of the instance so thiswithin the function will be a reference to the instance. So you want:
此处标识符名称用作变量,但您要查找的标识符是实例的命名属性,因此您需要按原样访问它。通常,此函数将作为实例的方法被调用,因此函数内的this将是对实例的引用。所以你要:
return "Hi" + otherName + ", my name is " + this.name;
So now when you can do (note that variables starting with a capital letter are, by convention, reserved for construtors):
所以现在你可以这样做(请注意,按照惯例,以大写字母开头的变量是为构造函数保留的):
> var john = new Person("John");
and then:
接着:
john.greet('Fred');
because greetis called as a method of john, it will return:
因为greet被作为john 的一个方法调用,它会返回:
Hi Fred, my name is John
回答by Sean Halls
Alternatively, since this is an issue of scope inheritance (second function not having access to the variable "name"), we can rephrase the code to look like this to include it all under the Person function:
或者,由于这是作用域继承的问题(第二个函数无法访问变量“name”),我们可以将代码改写成这样,将其全部包含在 Person 函数下:
function Person(name){
this.name = name;
this.greet = function(otherName){
return "Hi" + otherName + ", my name is " + name;
}
}
Works as well.
也有效。
回答by moho salad
Try the following:
请尝试以下操作:
function Person(name){
this.name = name;
this.greet = function(otherName){
return "Hi " + otherName + ", my name is " + name;
}
}
Person("Joe")
greet("Kate")
回答by kennypu
You need to change the greet function to use the object's name with the this
keyword:
您需要更改 greet 函数以使用带有this
关键字的对象名称:
Person.prototype.greet = function(otherName){
return "Hi" + otherName + ", my name is " + this.name;
}
after that, just call John.greet("other name");
之后,只需致电 John.greet("other name");