[英]member function in javascript 'function'/object is saying it is not a function
I am trying to review some basic Javascript using an online environment where print() is allowed. 我正在尝试使用允许print()的在线环境来复习一些基本的Javascript。 However, when I try to call say() on an instance of the person, it is saying that "say" is not a function.
但是,当我尝试在一个人的实例上调用say()时,它说的是“ say”不是一个函数。 I have tried this in several different environments with their version of print (be it writeln, console.log, or whatever) and it's always saying it's not a function.
我已经在几种不同的环境中用他们的print版本(无论是writeln,console.log或其他版本)尝试了这一点,并且总是说这不是一个功能。
//JavaScript-C24.2.0 (SpiderMonkey)
print("Hello, world!")
function person(name, age) {
this.name = name;
this.age = age;
this.say = function () {
print(this.name);
}
}
var arrayz = [];
arrayz.push(person("bob",20));
arrayz.push(person("sally",19));
arrayz.push(person("joe",22));
for (var z in arrayz) {
z.say();
}
I'm doing it in just the same manner as this is: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object-oriented_JS 我这样做的方式与此相同: https : //developer.mozilla.org/zh-CN/docs/Learn/JavaScript/Objects/Object-directional_JS
So I don't see what I'm doing wrong. 所以我看不到我在做什么错。 Can someone please help me with what I'm doing wrong here?
有人可以帮我解决我在这里做错的事情吗?
The correct code should be: 正确的代码应为:
print("Hello, world!")
function person(name, age) {
this.name = name;
this.age = age;
this.say = function () {
print(this.name);
}
}
var arrayz = [];
arrayz.push(new person("bob",20));
arrayz.push(new person("sally",19));
arrayz.push(new person("joe",22));
for (var z of arrayz) {
z.say();
}
Let me explain. 让我解释。 The first difference is the
new
keyword. 第一个区别是
new
关键字。 This creates a new instance of the function you've made as an object. 这将创建您作为对象创建的函数的新实例。 The way you did it just called
person
as a function. 您所做的方式只是将
person
称为函数。 So it stores the returned value of person
into the the array instead of the object instance. 因此它将返回的
person
值存储在数组中,而不是对象实例中。 Since person
doesn't return a value it stores undefined
into your array. 由于
person
不返回值,因此它将undefined
的值存储到数组中。
More info about the new
operator: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new 有关
new
操作员的更多信息: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new
Second, you used var z in arrayz
. 其次,您
var z in arrayz
使用了var z in arrayz
。 When using in
, z
becomes the index instead of the object. 当使用
in
, z
成为索引而不是对象。 If you want the z
to be the value of the array item you should use var z of arrayz
如果希望
z
为数组项的值,则应使用var z of arrayz
More info about for...in
: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in 有关
for...in
更多信息for...in
: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/for...in
More info about for...of
: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of 有关
for...of
更多信息: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/for...of
Using new person(...)
instead of person(...)
will create the object you want, filled with properties you specified by writing this.xxx = yyy
in the function body. 使用
new person(...)
代替person(...)
将创建所需的对象,并填充您通过在函数体内编写this.xxx = yyy
指定的属性。
You are simply calling the function, meaning that you are trying to get its the return value and push it in the array. 您只是在调用该函数,这意味着您试图获取其返回值并将其推入数组。
I don't see any return statement in your function person
, so the result is undefined
. 我在您的功能
person
中看不到任何return语句,因此结果是undefined
。
You have pushed 3 undefined
in your array, and you try to execute say()
on them. 您已将3
undefined
推入数组,然后尝试对它们执行say()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.