Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing arguments when using Object.create as opposed to new

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??

like image 410
Sethen Avatar asked Dec 25 '12 04:12

Sethen


People also ask

What is the difference between object create vs New?

The object used in Object. create() actually forms the prototype of the new object, whereas in the new Function() from the declared properties/functions do not form the prototype. You cannot create closures with the Object.

How do you pass an argument to an object?

To pass an object as an argument we write the object name as the argument while calling the function the same way we do it for other variables. Syntax: function_name(object_name); Example: In this Example there is a class which has an integer variable 'a' and a function 'add' which takes an object as argument.

Why do we use new in object creation?

The Java new keyword is used to create an instance of the class. In other words, it instantiates a class by allocating memory for a new object and returning a reference to that memory. We can also use the new keyword to create the array object.

Why do objects create null?

create(null) doesn't inherit from anything and thus has no properties at all. In other words: A javascript object inherits from Object by default, unless you explicitly create it with null as its prototype, like: Object. create(null) . {} would instead be equivalent to Object.


1 Answers

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

like image 91
Sethen Avatar answered Sep 18 '22 19:09

Sethen