2

For the below code,

           var customer={
                name: "Tom Smith",

                speak: function(){
                    return "My name is " + this['name'];
                },

                address: {
                    street: '123 Main St',
                    city: "Pittsburgh",
                    state: "PA"
                }
            };

Below is my visualisation of customer object,


enter image description here

My question:

Does customer object inherit properties(built-ins) of Object.prototype only?

Are the properties(built-ins) of Object function type object are also for the purpose of inheritance?

If yes, What is the syntax for customer object to inherit Object properties?

8
  • Possible duplicate of How does JavaScript .prototype work? Commented Dec 2, 2015 at 18:23
  • Anyhow, objects don't really inherit properties so much as they have a prototype chain of objects which property access will traverse. Commented Dec 2, 2015 at 18:25
  • @AlexanderO'Mara the suggested answer use [[Prototype]], which in my case, customer.[[Prototype]] is Object.prototype. But my question is, Does JS allow customer inherit Object properties as well? Is this a duplicate query? Commented Dec 2, 2015 at 18:29
  • Oh, you mean you the static-like methods of the Object constructor? They are not inherited, and normally you wouldn't need to do this, they are globally accessible. Commented Dec 2, 2015 at 18:31
  • @overexchange how did you make that visualization? Commented Dec 2, 2015 at 18:57

1 Answer 1

2

Below is my visualisation of customer object

It would be better if you used the term [[prototype]] instead of __proto__ - as you can see, .__proto__ is just a getter/setter inherited from Object.prototype.

Does customer object inherit properties(built-ins) of Object.prototype only?

Yes. Though you could add your own properties to Object.prototype, they would be inherited as well, it's not only the built-in ones.

Are the properties(built-ins) of Object are also for the purpose of inheritance?

No. They're static functions that are supposed to be called with objects as their arguments, not object methods.

What is the syntax for customer object to inherit Object properties?

Object is a function and usually you don't want to inherit from it. If you really wanted, you could use Object.create(Object).

Also, ES6 adds a new way to do that, as classes inherit static methods from their parent as well:

class MyObject extends Object {
    static myCreate(x) {
        return this.create(x);
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Do you see any mistake in above screenshot? apart from [[Prototype]] naming convention?
@overexchange: Customer should be customer. And you definitely need to do something about the arrows, though that's only a usability concern not one of correctness
Arrows? I did not get u.
I found those arrows between the boxes to be very hard to decipher visually. Those two crossings, uh, it's only one, and the merge, look too similar…
After your suggestions, Is this fine now?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.