Henry Dang
Henry Dang

Reputation: 863

Javascript - getPrototypeOf doesn't return the parent prototype

I have the following Javascript snippet:`

        var alien = {
            kind: "alien"
        };

        var person = {
            kind: "human"
        };

        var zack = Object.create(person);

        alert(Object.getPrototypeOf(zack));

    </script>

`

Not sure why it show zack's prototype is Object instead of person. Thanks enter image description here

Upvotes: 0

Views: 184

Answers (4)

Pankaj Shukla
Pankaj Shukla

Reputation: 2672

The reason you see [object Object] is because when you alert Object.getPrototypeOf(zack), the object needs to convert into string to be shown in the alert. Now, default toString() method Object.prototype.toString is being called that returns the string representation of an object. If you want to really display some meaningful text in the alert, you need to implement toString method on the person object itself. Doing this would show your own text implemented in toString() function. The reason why this would happen is the way function lookup happens in javascript. First, object is looked into, then it's prototype and then further into prototype chain. Since in this case lookup would succeed at the object level itself, Object.prototype.toString function won't be called and you would see your own text in alert box.

var alien = {
  kind: "alien"
};

var person = {
  kind: "human"
};
person.toString = function () {
	return "I am '"+this.kind+"' kind of object";
}

var zack = Object.create(person);

alert(Object.getPrototypeOf(zack));

Upvotes: 1

traktor
traktor

Reputation: 19301

The screen shot is showing the text returned by the Object.prototype.toString method. The method is called automatically if an object, constructed by Object, needs to be converted to text:

console.log( {} + "" ); // automatic convertion
console.log( {}.toString() ); // explicit convertion

What it is not saying is that the prototype of zack is Object. If you want more information about an object you can use JSON.stringify to create a string listing of all its non-function valued properties and their values. (JSON.stringify ignores function valued properties by design.)

You could also use equality operators to test if one object is the same as another, e.g.:

var alien = {
    kind: "alien"
};
var person = {
    kind: "human"
};
var zack = Object.create(person);

console.log( "zack is prototyped on person: " 
    + (Object.getPrototypeOf( zack) === person)
);

Upvotes: 0

Michael Fuller
Michael Fuller

Reputation: 177

Give this a try. I cleaned up your code a bit and used the standard naming conventions of classes start with an Uppercase. Also using the console to log, alerts are so yesterday.

// Define the Alien class, always start with an uppercase
var Alien = function() {

};

// Setup it's prototype
Alien.prototype = {
  kind: 'alien',
  grab: function() {
    return 'grabby grab';
  }
}

// Define a Person class
var Person = function() {
    this.kind = "human";
};

// Copy Alien prototype to Person
Person.prototype = Object.create(Alien.prototype);

// Create a new instance of of Person
var zack = new Person();

// See what's inside
console.log('Prototype function call: ' + zack.grab());
console.log('Zack is a: ' + zack.kind);
console.log('Zack is secretly a: ' + Object.getPrototypeOf(zack).kind);

console.log(Object.getPrototypeOf(zack));

Upvotes: 1

prasanth
prasanth

Reputation: 22500

its also person .But person is an object.its not display with alert() try with console.log() .If you need to show with in alert use with JSON.stringify()

How can I view an object with an alert()

var alien = {
  kind: "alien"
};

var person = {
  kind: "human"
};

var zack = Object.create(person);

console.log(Object.getPrototypeOf(zack));

alert(JSON.stringify(Object.getPrototypeOf(zack)))

Upvotes: 1

Related Questions