【问题标题】:how to make a method work - Javascript如何使方法工作 - Javascript
【发布时间】:2017-02-04 21:47:39
【问题描述】:

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

" hello - 此方法将字符串 "Hello, my name is" 记录到控制台 后面是演员的名字。 "

有人能帮我找出这段代码的错误吗? :

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();

提前非常感谢您;)

【问题讨论】:

  • 旁白:“你得了多少奥斯卡奖?” – “假” – 看起来像一个奇怪的值。如果你没有奥斯卡奖,奥斯卡奖数0,而不是false
  • 您在将 fn greeting 分配给 hello 时混淆了函数体语法和对象方法声明。您只需将该 fn 分配给构造函数中的 this.hello 属性,就像您对 firstNamelastName 和其他属性所做的那样

标签: javascript arrays object methods


【解决方案1】:

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

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);    
  }
}

【讨论】:

  • 使用prototype 会更好。
【解决方案2】:

hello改为

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

遍历actors并调用hello方法

actors.forEach(function(item) {

  item.hello();
})

JSFIDDLE

【讨论】:

  • 使用prototype 会更好。
【解决方案3】:

首先定义你的函数,比如

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');
}

【讨论】:

    【解决方案4】:
    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();
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-13
      • 2016-04-20
      • 1970-01-01
      • 1970-01-01
      • 2015-12-01
      • 1970-01-01
      • 2016-03-15
      • 2017-08-06
      相关资源
      最近更新 更多