This question already has answers here:
__proto__ VS. prototype in JavaScript
(34 answers)
Closed 2 years ago.
My question is Object is last on prototype chain all objects inherit properties and methods from it how it is inherit methods from Function.prototype, why Object.__proto__ === Function.prototype // true, why Object.__proto__ is not null.
The fundamental Object in JavaScript has to have a way for it to be instantiated, so it requires a prototype object to aid in lines like this:
let myObj = new Object();
And that is why Object.__proto__ is not null.
The Function object is a special type of object that facilitates object instantiation via a constructor and when used this way is known as a "constructor function". So, it makes sense for Object to inherit from a Function object so that object instances can be made.
Function is also an object. So it inherits from Object too.
But don't be confused by Object.__proto__. That's something the browser adds and isn't actually part of the language. So don't rely on it or use it.
Essentially, Function.prototype is Object.__proto__.
Related
This question already has answers here:
Difference between __proto__ and [[prototype]] in Global-Object?
(2 answers)
Closed 11 months ago.
What does the [[Prototype]]: Object string in the browser console (see pic) mean. As you can see from the code below:
({}).__proto__.__proto__ === null
the prototype chain consists of 2 links, but in the browser console for some reason my {} turned into [[Prototype]]: Object.
[[Prototype]] is the name of the internal field of an object that says what that object's prototype is.
The prototype is an object that is associated with every functions and objects by default in JavaScript, where function's prototype property is accessible and modifiable and object's prototype property (aka attribute) is not visible.
The prototype object is special type of enumerable object to which additional properties can be attached to it which will be shared across all the instances of it's constructor function.
This question already has answers here:
Why were ES5 Object methods not added to Object.prototype?
(2 answers)
Why is it Object.defineProperty() rather than this.defineProperty() (for objects)?
(3 answers)
Closed 1 year ago.
Object sits at the very base of the JS Data-Structures. Whatever other data-types we say (Arrays, Promises, Date, Strings, Numbers, Functions, Boolean, Maps, Sets, etc.) --- have Object as their upstream in the prototype chain so they may access all its properties and methods.
Now, even Object has got a set of its own properties & methods, which it might access when it needs.
Object.prototype gives set of its properties and methods.
Object.assign Now returns some function.
The strange thing is I expected assign() function to be a method up in the Object-prototype chain. Why is it not present? Shouldn't ideally be it should be there ?? If NOT, how object is able to use it?
Also, Object.prototype.prototype will give you null as there is nothing above it.
consider:
This question already has answers here:
Why were ES5 Object methods not added to Object.prototype?
(2 answers)
Closed 22 days ago.
I'm working on a JavaScript project, and was just wondering why an object instance doesn't inherit the assign() and other methods, rather than having to call the superclass (superobject?) Object method.
Why is it Object.assign() rather than this.assign() (for objects)?
Because of the way inheritance works in JavaScript. Quoting from
JavaScript For Beginners: the ‘new’ operator
When you use new, four things happen:
It creates a new, empty object.
It binds this to our newly created
object.
It adds a property onto our newly created object called
“proto” which points to the constructor function’s prototype
object.
It adds a return this to the end of the function, so that the
object that is created is returned from the function.
So the super object method's aren't copied to the new object. They're accessible through the newly created object's prototype.
This question already has answers here:
Function nested itself in the prototype
(2 answers)
Closed 4 years ago.
I was inspecting the proto of an object created via function constructor. I am curious to understand the reasoning behind never-ending nesting of constructor->prototype inside the proto
Code is:
var Shape = function(dimensions) {
this.dimensions = dimensions;
this.printShape = function() {
console.log("I have" + this.dimensions + "dimensions");
}
}
var square = new Shape(4);
Screenshot of Nesting:
That's not a never-ending series! The constructor of one is the prototype of the other.
JavaScript supports for a more powerful type of inheritance called prototype inheritance.
Any object can have a prototype object associated with it. The prototype object, can have its own prototype object. This sequence of prototypes makes up the object’s prototype chain.
This question already has answers here:
__proto__ VS. prototype in JavaScript
(34 answers)
Closed 7 years ago.
Constructor:
function Team (type) {
this.type = type;
}
//this will output this empty object inherited from Object.property
console.log(Team.prototype);
-> Team {}
//this one outputs nothing in my console
console.log(Object.getPrototypeOf(Team));
//is it inheriting from this one, the one for all functions?
-> Function.prototype //??
What is the difference between the .prototype property and Object.getPrototypeOf?
What's else does Function.prototype (the one that all functions and constructors inherit from) prototype do, except for storing properties?
Team is a function, so it inherits all the properties from Function.prototype. Function is also an (inherits from) object, so it has all the properties from Object.prototype. However, Object.getPrototypeOf is a "static" method on Object, so it's no inherited.
Object.getPrototypeOf(Team) points to the same object as Function.prototype. Team.getPrototypeOf is undefined.