do.
do.

Reputation: 617

The relationship between __proto__ of an instance and the prototype of its constructor in javascript

i wrote the code to figure out the relationship between __proto__ of an instance and the prototype of its constructor in javascript for me:

// Constructor
var Guy = function( name ) {
       this.name = name;
};

// Prototype
var chinese = {
        region: "china",
        myNameIs: function() {
            return this.name;
        }
};

Guy.prototype = chinese;

var he = new Guy( "Wang" );
var me = new Guy( "Do" );

i was given a false as i tested whether me.__proto__ is equal to chinese:

console.log( "__proto__ of me is chinese? " + chinese == me.__proto__ ); // logs false

Why were they not the same thing?

Upvotes: 2

Views: 134

Answers (1)

user1106925
user1106925

Reputation:

Because + has higher precedence than ==, so you're really doing...

( "__proto__ of me is chinese? " + chinese ) == me.__proto__

what you need to do is...

"__proto__ of me is chinese? " + ( chinese == me.__proto__ )

or use a , in the console call to pass separate arguments...

"__proto__ of me is chinese? ", chinese == me.__proto__ 

Upvotes: 3

Related Questions