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?
Static methods
Object.assign()
Object.create()
Object.defineProperty()
Object.defineProperties()
Object.entries()
Object.freeze()
Object.fromEntries()
Object.getOwnPropertyDescriptor()
Object.getOwnPropertyDescriptors()
Object.getOwnPropertyNames()
Object.getOwnPropertySymbols()
Object.getPrototypeOf()
Object.is()
Object.isExtensible()
Object.isFrozen()
Object.isSealed()
Object.keys()
Object.preventExtensions()
Object.seal()
Object.setPrototypeOf()
Object.values()
Instance properties
Object.prototype.constructor
Object.prototype.proto
Instance methods
Object.prototype.defineGetter()
Object.prototype.defineSetter()
Object.prototype.lookupGetter()
Object.prototype.lookupSetter()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toString()
Object.prototype.valueOf()
(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.
CodePudding user response:
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"
}
CodePudding user response:
Static methods cannot be inherited, because they exist on the class and not on any instance of a class. So those are out. The other methods, absolutely! At least I think so. Let's run some experiments and check the results.
const f = () => {
return "hello world"
};
f.foo = "bar";
console.log(f.constructor);
console.log(f.toString());
console.log(f.valueOf());
console.log(f.hasOwnProperty("foo"));
console.log(f.hasOwnProperty("frankincense"));
This snippet indicates that, yes indeed, non-static methods are inherited by functions, because functions are objects... as Crockford rolls in his grave