7

This question isn't a duplicate of Using "Object.create" instead of "new". The thread in question doesn't focus on passing arguments correctly when using Object.create


I am curious as to how I would go about initializing objects using Object.create as opposed to new. Here is my code so far:

function Human(eyes) {
    this.eyes = eyes || false;
}

Human.prototype.hasEyes = function() {
    return this.eyes;
}

function Male(name) {
    this.name = name || "No name";
}

Male.prototype = new Human(true); //passing true to the Human constructor

var Sethen = new Male("Sethen");

console.log(Sethen.hasEyes());

As you can see above, the Male.prototype = new Human(true); creates a new object with true. When the hasEyes() function is run, this logs true as expected.

So, my question is.. using Object.create how would I go about doing this the same way passing a true parameter??

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sethen
  • 11,140
  • 6
  • 34
  • 65
  • Dup: http://stackoverflow.com/questions/2709612/using-object-create-instead-of-new – elclanrs Dec 25 '12 at 04:19
  • This isn't an exact duplicate by the way, the thread in question focuses on using `Object.create` over `new` where I am trying to focus on passing arguments with each of them, something that thread doesn't cover. – Sethen Dec 25 '12 at 16:55
  • you could do this like this: http://jsbin.com/umodef/1/edit – Moritz Roessler Dec 25 '12 at 17:07
  • @Glutamat I guess I am not sure how this applies to the question. Did you see the answer I provided? This seems to be the way to go about this. – Sethen Dec 25 '12 at 18:42

2 Answers2

8

You must call the constructor using Object.call(this) and then pass your arguments.

function Human(eyes, phrase) {
    this.eyes = eyes || false;
    this.phrase = phrase;
}

Human.prototype.hasEyes = function() {
    return this.eyes;
}

Human.prototype.sayPhrase = function() {
    return this.phrase;
}

function Male(name) {
    Human.call(this, true, "Something to say"); //notice the call and the arguments
    this.name = name || "No name";
}

Male.prototype = Object.create(Human.prototype);

var Sethen = new Male("Sethen");

console.log(Sethen.hasEyes());
console.log(Sethen.sayPhrase());

console.log(Object.getOwnPropertyNames(Sethen));

This works and now the object Male has the properties of eyes and phrase

Sethen
  • 11,140
  • 6
  • 34
  • 65
0

is this give some clue about prototype inheritance model.

const Human = {
    constructor: function (eyes) {
        this.eyes = eyes || false;
    },
    hasEyes: function () {
        return this.eyes;
    }
};

let Male = Object.create(Human);

Male.constructor = function (name) {
    this.name = name || "No name";
    return Human.constructor.call(this, "true");
};

let Sethen = Object.create(Male);
Sethen.constructor = function() {
    return Male.constructor.call(this, 'Sethen');
};

Sethen.constructor();

console.log(Sethen.hasEyes());
console.log(Object.getOwnPropertyNames(Sethen));
zabusa
  • 2,520
  • 21
  • 25