简体   繁体   English

javascript'function'/ object中的成员函数表示它不是函数

[英]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. 当使用inz成为索引而不是对象。 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...inhttps : //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

Solution : 解决方案:

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指定的属性。

Explanation of your error : 您的错误的说明:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM