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?

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • Possible duplicate of [How does JavaScript .prototype work?](http://stackoverflow.com/questions/572897/how-does-javascript-prototype-work) – Alexander O'Mara Dec 02 '15 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. – Alexander O'Mara Dec 02 '15 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? – overexchange Dec 02 '15 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. – Alexander O'Mara Dec 02 '15 at 18:31
  • @overexchange how did you make that visualization? – zer00ne Dec 02 '15 at 18:57
  • @zer00ne Using `msword` and `mspaint`. I still missed `Function` object, which is not relevant here. Any reason for this question? I have such visualisations on OOJS [here](https://github.com/shamhub/Javascript_Programming/tree/master/Topic7_WorkingWithObjects). – overexchange Dec 02 '15 at 18:59
  • @overexchange I have trouble trying to explain things to my boss sometimes and hoping it was from a program not talent :P It would take me forever to produce quality like that. – zer00ne Dec 02 '15 at 19:03
  • @overexchange Watched, starred, & forked, thank you sir. – zer00ne Dec 02 '15 at 19:23

1 Answers1

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);
    }
}
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375