将所有构造函数对象合并到数组中,然后为所有这些对象调用原型

时间:2017-01-17 20:37:50

标签: javascript

因此,确保有一种更好的方法来为构造函数对象调用原型。 比通过名称obj.getValue()调用它们; obj2.getValue ;.这是我希望将它们包装到数组中的代码示例。并立即调用所有这些的原型

function inherits(ctor, superCtor) {
  ctor.super_ = superCtor;
  ctor.prototype = Object.create(superCtor.prototype, {
    constructor: {
      value: ctor,
      enumerable: false,
      writable: true,
      configurable: true
    }
  });
};

var Person = function(name, age, value, arr) {
  this.name = name;
  this.age = age;
  this.value = function() {
    var values = document.getElementsByTagName('input');
    var valu = values.value;
    return {
      value: this.valu,
    }
  };
  this.arr = [];
}

Person.prototype.getName = function() {
  var div = document.createElement('div');
  var txt = document.createTextNode(this.name + this.age);
  div.appendChild(txt);
  body = document.querySelector('body');
  body.appendChild(div);
}

Person.prototype.getVal = function() {
  this.value();
  this.arr.push(this.value);
}

var Friend = function(name, age, value, arr, love) {
  Friend.super_.call(this, name, age, value);
  this.love = love;
}

inherits(Friend, Person);

Friend.prototype.getLove = function() {
  console.log(this.love);
}

var mainObje = [{
  duka: new Person('nemke', 12),
}, {
  vaske: new Friend('duka', 24, 'huge'),
}, ]

for (i = 0; i < mainObje.length; i++) {
  console.log(mainObje[i]);
  for (var key in mainObje[i]) {
    console.log(key);
  }
}

console.log(typeof mainObje)

0 个答案:

没有答案