Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7686527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:30:04+00:00 2026-05-31T19:30:04+00:00

When evaluating the value of myObject.myMember My guess is that javascript will try to

  • 0

When evaluating the value of
myObject.myMember

My guess is that javascript will try to look up the “myMember” entry in:


    myObject
    myObject.constructor.prototype
    myObject.constructor.prototype.constructor.prototype
    myObject.constructor.prototype.constructor.prototype.constructor.prototype
    .......

until succeed.

The following code seems to validate my guess

var MyClass1=function(){
    this.key1="value1"; 
};
var myObject=new MyClass1();

MyClass1.prototype.key2="value2";

console.log(myObject.constructor.prototype.key2=="value2"); //true
console.log(myObject.key2=="value2"); //true   :)

But the following code proves my guess is wrong

var MyClass1=function(){
    this.key1="value1"; 
};

var MyClass2=function(){
    this.key2="value2"; 
};

var myObject=new MyClass1();

MyClass1.prototype=new MyClass2();
console.log(myObject.constructor.prototype.key2=="value2");//true
console.log(myObject.key2=="value2");// false! why?

What is the actual algorithm by which javascript looks up a member of an object through its prototype chain?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:30:05+00:00Added an answer on May 31, 2026 at 7:30 pm

    It’s because you called MyClass1.prototype=new MyClass2(); after creating an instance of MyClass1.

    var MyClass1=function(){
        this.key1="value1"; 
    };
    
    var MyClass2=function(){
        this.key2="value2"; 
    };
    
    MyClass1.prototype=new MyClass2();
    
    var myObject=new MyClass1();
    
    myObject.key1 === "value1";
    myObject.key2 === "value2";
    

    Let’s inspect the operation var myObject=new MyClass1()

    The new operator ( http://es5.github.com/#x11.2.2 ) calls the [[Construct]] internal method of myClass1 (pseudo-code, of course.)

    This is the skimmed version of what happens inside [[Construct]] ( http://es5.github.com/#x13.2.2 ) calls (obviously pseudo-code):

    //fun in our operation is MyClass1
    function Construct ( fun ) {
        //we create a fresh new object
        var obi = makeNewObject();
    
        //grab the prototype of MyClass1
        var proto = fun.prototype;
        //and set the actual prototype of obi to that prototype
        setPrototype( obi, proto );
    
        //and then returning the created and altered object, that will be myObject
        return obi;
     }
    

    By “actual prototype”, I mean not the fun.prototype you’re used of seeing – that prototype looks forward, to instances we will create. The [[Prototype]] of an object is the properties inherited to it, it’s its past.

    That can be retrieved by using Object.getPrototypeOf, and in some implementations can be set using the non-standard obj.__proto__.

    Okay, now that we’ve established how objects are created using new, why doesn’t your example work?

    Replacing MyClass1.prototype after you create myObject will break the link that was between the two. MyClass1 has moved to the dark side, it has completely changed, a new object now replaces it, and myObject wants nothing to do with that.

    Before the change, myObject.__proto__ === MyClass1.prototype, it holds a reference to the object MyClass1.prototype. Once you’ve changed the object MyClass1.prototype, myObject.__proto__ will continue referencing that old object instead of the new one.

    The answer is pretty much done. To rehash – The [[Construct]] function establishes a link between MyClass1.prototype and myObject. That link doesn’t change when you replace MyClass1.prototype, so myObject will happily live with the object it was originally linked to.

    In case you’re still curious about how property fetching is done: http://es5.github.com/#x8.12.2 I warn you though, spec-language is tedious to read.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm evaluating and looking at using CherryPy for a project that's basically a JavaScript
I'm trying to run a basic loop that will find a specific value in
We are currently evaluating different applications that interface with Visual Studio 2008 (C#) and
I'm currently evaluating CodeRush and one thing that I liked most when reading the
Question: does operand order make any difference when evaluating equality or identity in JavaScript?
It seems that Mathematica's handling of principal value integrals fails on some corner cases.
What is the motivation for Scala assignment evaluating to Unit rather than the value
I realize that evaluating an array in Perl in a scalar context is useful:
When evaluating the value of a cell in a worksheet in Excel, is the
I'm in the process of evaluating how to implement something using a distributed key/value

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.