What's the difference between "prototype" and "__proto__"?
-
What's the difference between "prototype" and "__proto__" in JavaScript?
-
What's the difference between "prototype" and "__proto__" in JavaScript?
-
What's the difference between "prototype" and "__proto__" in JavaScript?
__proto__ - __proto__ is the actual object that is used in the lookup chain to resolve methods. - It is a property that all objects have. This is the property which is used by the JavaScript engine for inheritance. - According to ECMA specifications it is supposed to be an internal property, however most vendors allow it to be accessed and modified. Syntax
var Circle = function () {};
var shape = {};
var circle = new Circle();// Set the object prototype.
// DEPRECATED.This is for example purposes only. DO NOT DO THIS in real code.
shape.__proto__ = circle;// Get the object prototype
console.log(shape.__proto__ === circle); // trueprototype - prototype is a property belonging only to functions. - It is used to build __proto__ when the function happens to be used as a constructor with the new keyword. - In prototype-based object oriented languages like Self and Javascript, every object in the system has a field that says "if I don't have a property or method that is requested of me, go to the object that this field references my prototype and look for it". - Since that object will also have this "prototype" field as well, this becomes a recursive process. - It is what is meant by a prototype chain. - Note that this means that in a prototype language, there is no abstract concept of a "class" Syntax
var shape = function () {};
var p = {
a: function () {
console.log('aaa');
}
};
shape.prototype.__proto__ = p;var circle = new shape();
circle.a(); // aaa
console.log(shape.prototype === circle.__proto__); // true// or
var shape = function () {};
var p = {
a: function () {
console.log('a');
}
};var circle = new shape();
circle.__proto__ = p;
circle.a(); // a
console.log(shape.prototype === circle.__proto__); // false// or
function test() {};
test.prototype.myname = function () {
console.log('myname');
};var a = new test();
console.log(a.__proto__ === test.prototype); // true
a.myname(); // myname// or
var fn = function () {};
fn.prototype.myname = function () {
console.log('myname');
};var obj = {
__proto__: fn.prototype
};obj.myname(); // myname
-
__proto__ - __proto__ is the actual object that is used in the lookup chain to resolve methods. - It is a property that all objects have. This is the property which is used by the JavaScript engine for inheritance. - According to ECMA specifications it is supposed to be an internal property, however most vendors allow it to be accessed and modified. Syntax
var Circle = function () {};
var shape = {};
var circle = new Circle();// Set the object prototype.
// DEPRECATED.This is for example purposes only. DO NOT DO THIS in real code.
shape.__proto__ = circle;// Get the object prototype
console.log(shape.__proto__ === circle); // trueprototype - prototype is a property belonging only to functions. - It is used to build __proto__ when the function happens to be used as a constructor with the new keyword. - In prototype-based object oriented languages like Self and Javascript, every object in the system has a field that says "if I don't have a property or method that is requested of me, go to the object that this field references my prototype and look for it". - Since that object will also have this "prototype" field as well, this becomes a recursive process. - It is what is meant by a prototype chain. - Note that this means that in a prototype language, there is no abstract concept of a "class" Syntax
var shape = function () {};
var p = {
a: function () {
console.log('aaa');
}
};
shape.prototype.__proto__ = p;var circle = new shape();
circle.a(); // aaa
console.log(shape.prototype === circle.__proto__); // true// or
var shape = function () {};
var p = {
a: function () {
console.log('a');
}
};var circle = new shape();
circle.__proto__ = p;
circle.a(); // a
console.log(shape.prototype === circle.__proto__); // false// or
function test() {};
test.prototype.myname = function () {
console.log('myname');
};var a = new test();
console.log(a.__proto__ === test.prototype); // true
a.myname(); // myname// or
var fn = function () {};
fn.prototype.myname = function () {
console.log('myname');
};var obj = {
__proto__: fn.prototype
};obj.myname(); // myname