简体   繁体   English

无法调用在javascript中的对象内部定义的方法

[英]Unable to call a method defined inside an object in javascript

i have defined an object containing an array of objects and functions and called the methods as below: 我已经定义了一个包含对象和函数数组的对象,并调用了以下方法:

 var Person = { people: [{ name: "Max", age: 41, }, { name: "John", age: 25, }, { name: "Doe", age: 67, }, ], greeting: function() { return "Hello, my name is" + " " + this.name; } }; function searchByName(namePerson) { var result = Person.people.find(Obj => Obj.name === namePerson); return result; } var max = searchByName('Max'); max.greeting(); 

Is something wrong with my function definition? 我的函数定义有问题吗? On running it says "greeting" is not a function. 运行时说“打招呼”不是功能。

Your code doesn't make much sense, your greeting function is on the outer Person object but you are using it as if it were a property of each person inside the array. 您的代码没有多大意义,您的greeting函数在外部的Person对象上,但是您正在使用它,就好像它是数组中每个人的属性一样。

You need a constructor for persons so you can instantiate three Person s with a greeting method 您需要一个用于人员的构造函数,以便可以使用greeting方法实例化三个Person

 function Person(name, age) { this.name = name; this.age = age; } Person.prototype.greeting = function() { return "Hello, my name is " + this.name + " and I am " + this.age + " years old"; } const people = [ new Person("Max", 41), new Person("JOhn", 25), new Person("Doe", 67), ]; function searchByName(namePerson) { var result = people.find(obj => obj.name === namePerson); return result; } var max = searchByName('Max'); console.log(max.greeting()); 

You could change your Person into an actual class that you could instantiate with new Person() 您可以将您的Person更改为可以使用new Person()实例化的实际类

 //make the person class function Person ( person ) { this.name = person.name; this.age = person.age; } //add the greeting method to the person class Person.prototype.greeting = function () { return "Hello, my name is" + " " + this.name; }; //build your people var people = [ new Person( { name: "Max", age: 41 } ), new Person( { name: "John", age: 25 } ), new Person( { name: "Doe", age: 67 } ) ]; function searchByName(namePerson) { var result = people.find(person => person.name === namePerson); return result; } var max = searchByName('Max'); console.log( max.greeting() ); 

You're returning an object, which does not have a greeting function. 您要返回的对象没有问候功能。

A potential different solution with a little bit of a different structure is this. 这是一个潜在的,具有不同结构的解决方案。

 function Person(name, age) { this.name = name; this.age = age; } Person.prototype.greeting = function() { return "Hello, my name is " + this.name; } function People(personArr) { this.people = personArr; } People.prototype.searchByName = function(name) { return this.people.find( person => person.name === name); } var people = new People([new Person("Max", 41), new Person("John", 25), new Person("Doe", 67)]); var max = people.searchByName("Max"); console.log(max.greeting()); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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