javascriptoopinheritanceprototypal-inheritance

Are these inherited properties?


I am learning about inherited properties and I am pretty confused. I would really appreciate your help to give me some clarity.

So, an inherited property is a property that the object inherits from the prototype object.

An article I read gave an example of an inherited property: "every JavaScript object inherits the toString property from its prototype object"

So, my question is: Are these all inherited properties?

(I found this list from MDN objects, they are static methods, instance properties, and instance methods of objects, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)

And So, does that mean all objects have the above properties?

And since functions are basically objects, functions also inherited those?

Also, is there a place I can find the list of all the inherited properties objects have?

Thank you so much in advance! sorry for the long post.


Solution

  • The static methods, belong only to the Object class ( constructor function ), that means that you can call them only by doing Object.nameOfMethod().

    The instance properties & methods on the other hand, are inherited by all the instances of Object, which means that every object will inherit those props/methods.

    Now remember that in JS every entity if it's not a primitive, is an object.

    For example an array:

    const arr = ["hello"]
    
    console.log(typeof arr)                           // object
    console.log(arr instanceof Object)                // true
    console.log(Array.prototype.isPrototypeOf(arr))   // true
    console.log(arr.propertyIsEnumerable(0))          // true
    console.log(arr.toLocaleString())                 // hello
    console.log(arr.valueOf())                        // ["hello"]
    console.log(arr.hasOwnProperty(0))                // true
    console.log(arr.toString())                       // hello
    

    This is because even if arrays in JS are instances of Array, they are also instances of Object, you can see an array as a special object where indexes are keys:

    const arr = {
    0: "hello"
    }