nicoleJT914 / blog

一只游行的火烈鸟/用issues记博客
0 stars 0 forks source link

模拟new的功能 #37

Open nicoleJT914 opened 7 years ago

nicoleJT914 commented 7 years ago
function Otaku (name, age) {
    this.name = name;
    this.age = age;
    this.habit = 'Games';
}
Otaku.prototype.strength = 60;
Otaku.prototype.sayYourName = function () {
    console.log('I am ' + this.name);
}
var person = objectFactory(Otaku, 'jack', 18)
function objectFactory(constr) {
  var args = Array.prototype.slice.call(arguments, 1)
  var obj = Object.create(constr.prototype)
  constr.apply(obj, args)
  return obj
}