javascript构造函数涉及未知数量的参数...

时间:2015-05-21 06:16:36

标签: javascript constructor arguments

我在使用codecadamy时遇到了一些js,并试图让某些东西发挥作用。

本质上,我创建了一些加载到控制器对象中的对象,并将其设置为属性,使用两个函数打印属性,并将字符串与控制器中每个对象的name属性进行比较。

我注意到如果我在原型样式中创建对象并指定一个正常的函数来处理设置属性,我可以这样做:

var friends = {};

friends.setUp = function() {
  this.friends = [];
  for(var i in arguments) {
    arguments[i].setUp();
    this.friends.push(arguments[i]);
  }
};

friends.list = function() {
  for(var i in this.friends) {
    console.log(this.friends[i]);
  }
};

friends.search = function(name) {
  for(var i in this.friends) {
    if(this.friends[i].firstName === name) {
      return this.friends[i];
    }
  }
};

var bill = {};

bill.setUp = function() {
  this.firstName = "Bill";
  this.lastName = "Gates";
  this.number = "(206) 555-5555";
  this.address = ['One Microsoft Way','Redmond','WA','98052'];
};

var steve = {};

steve.setUp = function() {
  this.firstName = "Steve";
  this.lastName = "Jobs";
  this.number = "(206) 555-5555";
  this.address = ['1 Infinite Loop','Cupertino','CA','95014'];
};

var mike = {};

mike.setUp = function() {
  this.firstname = "Mike";
  this.lastname = "Ryd";
  this.number = "(800) 555-5555";
  this.address = ['redacted'];
};

friends.setUp(bill, steve, mike);
friends.list();
var result = friends.search("Steve");
console.log(result);

但是,如果我使用构造函数执行它不起作用,例如:

function bill() {
  this.firstName = "Bill";
  this.lastName = "Gates";
  this.number = "(206) 555-5555";
  this.address = ['One Microsoft Way','Redmond','WA','98052'];
};

function steve() {
  this.firstName = "Steve";
  this.lastName = "Jobs";
  this.number = "(206) 555-5555";
  this.address = ['1 Infinite Loop','Cupertino','CA','95014'];
};

function mike() {
  this.firstname = "Mike";
  this.lastname = "Ryd";
  this.number = "(800) 555-5555";
  this.address = ['redacted'];
};

function friends() {
  this.friends = [];
  for(var i in arguments) {
    this.friends.push(arguments[i]);
  }
};

friends.list = function() {
  for(var i in this.friends) {
    console.log(this.friends[i]);
  }
};

friends.search = function(name) {
  for(var i in this.friends) {
    if(this.friends[i].firstName === name) {
      return this.friends[i];
    }
  }
};

var bill = new bill();
var steve = new steve();
var mike = new mike();
var friends = new friends(bill, steve, mike);
friends.list();
var result = friends.search("Steve");
console.log(result);

我想知道这是否是使用构造函数的限制,还是我在某处弄乱了语法?谢谢!

1 个答案:

答案 0 :(得分:0)

这似乎与具有未知数量的参数的构造函数有关,而是您没有适当地为对象分配方法。需要将它们放在原型上,以便它们将由此特定构造函数创建的所有对象继承。所以在你的代码中,这些:

friends.list = function() {...}
friends.search = function() {...}

需要更改为:

friends.prototype.list = function() {...}
friends.prototype.search = function() {...}

像这样:

friends.prototype.list = function() {
  for(var i = 0; i < this.friends.length; i++) {
    console.log(this.friends[i]);
  }
};

friends.prototype.search = function(name) {
  for(var i = 0; i < this.friends.length; i++) {
    if(this.friends[i].firstName === name) {
      return this.friends[i];
    }
  }
};

然后,这段代码应该可以正常工作:

var bill = new bill();
var steve = new steve();
var mike = new mike();
var friends = new friends(bill, steve, mike);
friends.list();
var result = friends.search("Steve");
console.log(result);

然后,代码就像您期望的那样工作:http://jsfiddle.net/jfriend00/ba4me8ua/

仅供参考,您注意到我改变了您遍历arguments对象项的方式,使其更像数组,并避免在迭代中获得任何非数字属性的机会。