Infinite nesting of constructors in __proto__ javascript [duplicate] - javascript

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.

Related

javascript prototypes and inheritance [duplicate]

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__.

Polymorphism In javascript with prototype [duplicate]

This question already has an answer here:
What are the differences between Rectangle.prototype = Object.create(Shape.prototype) and Rectangle.prototype = Shape.prototype?
(1 answer)
Closed 4 years ago.
What does the second line of this code means?
var Square = function (){
Square.prototype = Object.create(Shape.prototype); // Meaning?
Square.prototype.draw = function (){
return "I am a square";
}
It's creating an object that uses the object Shape.prototype refers to as its prototype. This is so that instances created via new Square inherit from that object, which in turn inherits from Shape.prototype, and so those instances have the features of both.
It's incomplete, it should be followed by:
Square.prototype.constructor = Square;
Of course, since ES2015 came out and was well-supported by transpilers (nd now by modern browsers and other modern JavaScript environments), there's little reason to hook things up this verbose and error-prone way.

Is there a useful difference between defining a method in an object contructor and appending it to the constructor's prototype object? [duplicate]

This question already has answers here:
Use of 'prototype' vs. 'this' in JavaScript?
(15 answers)
Closed 7 years ago.
Consider this code example:
function Person(config) {
this.name = config.name;
this.age = config.age;
}
Person.prototype.getAge = function() {
return this.age;
};
var tilo = new Person({name:"Tilo", age:23 });
console.log(tilo.getAge());
Rather than defining getName() as an appendage to the constructor's prototype property, it seems to me it could be defined within the constructor to achieve the same thing? Is there any useful difference? In other words, does doing it one way or the other have any particular implementation advantage?
The constructor will create a new instance of that function with each invocation. Putting it on the prototype will save memory.

Why we use "prototype" property to add methods to constructor instances? [duplicate]

This question already has answers here:
Use of 'prototype' vs. 'this' in JavaScript?
(15 answers)
Closed 7 years ago.
I am creating a JavaScript constructor. Consider this example:
function Student(name)
{
this.name = name;
}
Student.prototype.printName = function(){
console.log(this.name);
}
var s = new Student("Eden");
s.printName();
This above code works all well. But I can write the same this way also:
function Student(name)
{
this.name = name;
this.printName = function(){
console.log(this.name);
}
}
var s = new Student("Eden");
s.printName();
I feel the second method is correct way because printName should be own property of new object not an inherited property. Adding to prototype makes it an inherited property.
Why do developer prefer the first method?
By attaching functions directly to this in a constructor, that means every instance of Student has it's own copy of printName. That isn't very efficient. By attaching it to the prototype chain of Student, there is only one copy of the printName function and any instance of Student has it in it's prototype chain.
While there are other differences in those two approaches for attaching function to objects, that is the simplest and easiest to remember.
If you want the full dictionary of reasons how the two differ, please refer to this answer to an extremely similar question.

Difference between the prototype property of a JS constructor and it's prototype [duplicate]

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.

Categories

Resources