如何使一个方法工作- Javascript

how to make a method work - Javascript

本文关键字:Javascript 工作 方法 何使一      更新时间:2023-09-26

我想添加以下方法到我的对象:

" hello -此方法向控制台记录字符串" hello,我的名字是"后面跟着演员的名字。"

有人能帮我找到我在哪里错了这个代码?:

function Person (firstName, lastName, age, numOscars) {
  this.firstName = firstName;
  this.lastName = lastName;
  this.age = age;
  this.numOscars = numOscars;
  hello : function greeting() {
        console.log ("Hello, my name is " + this.firstName);
  }
}
var actors = new Array ();
actors[0] = new Person ("Leonardo", "DiCaprio", 41, 1);
actors[1] = new Person ("Jennifer", "Lawrence", 25, 1);
actors[2] = new Person ("Samuel L."," Jackson", 67, false);
actors[3] = new Person ("Meryl", "Streep", 66, 3);
actors[4] = new Person ("John", "Cho", 43, false);
Person.hello();

hello改为

this.hello=function(){
  //Rest of code
}

遍历actor并调用hello方法

actors.forEach(function(item) {
  item.hello();
})

JSFIDDLE

首先定义函数

function sayHello (firstName, lastName, age, numOscars) {
      console.log("Hello my name is ", firstName," ", lastName, "I am", age, "years old, and I have ", numOscars, " Oscar(s).");
}

像这样创建你的演员数组,你可以添加更多的对象到它。

var actors = [{firstName: "Leonardo", lastName: "DiCaprio", age: "42", numOscars: 1}, {firstName: "example", lastName: "something", age: "12", numOscars: 0}];

,你可以单独调用你的函数,像

var leonardo = actors[0]
sayHello(leonardo.firstName, leonardo.lastName, leonardo.age, leonardo.numOscars);

或使用forEach:

actors.forEach(function(actor) {
   sayHello(actor.firstName, actor.lastName, actor.age, actor.numOscars);
   console.log(''n');
}
function Person(firstName, lastName, age, numOscars) {
  this.firstName = firstName;
  this.lastName = lastName;
  this.age = age;
  this.numOscars = numOscars;
}
  // If hello function is added to Person functions prototype
  // then each time a new Person object is created, 
  // hello function gets inherited rather than creating a new hello function
  Person.prototype.hello = function greeting() {
      // here firstName always refer to the specific object to which belongs
      console.log("Hello, my name is " + this.firstName);
  }
// instead of new Array()
// Use short hand form of Array declaration also called Array literal notation
var actors =[];

// create a new person object and add it to actors array
actors.push(new Person("Leonardo", "DiCaprio", 41, 1));
actors.push(new Person("Jennifer", "Lawrence", 25, 1));
actors.push(new Person("Samuel L."," Jackson", 67, false));
actors.push(new Person("Meryl", "Streep", 66, 3));
actors.push(new Person("John", "Cho", 43, false));
// loop through each of actors array members and call 
// hello function
actors.forEach(function(actor){
  actor.hello();
});

您可能是初学者:)您将对象文字表示法与构造函数混合在一起。构造函数的正确版本在

下面
function Person (firstName, lastName, age, numOscars) {    
  this.firstName = firstName;    
  this.lastName = lastName;    
  this.age = age;    
  this.numOscars = numOscars;
  this.hello = function () {
      console.log ("Hello, my name is " + this.firstName);    
  }
}