var obj = {ownProperty: 1}; console.log(obj.ownProperty);// 1
But if it is not there, the search takes place in the __proto__ property: obj.__proto__ = {propertyOfProto: 2}; console.log(obj.propertyOfProto);// 2
If it is not there, it is searched further down the chain: obj.__proto__.__proto__ = {propertyOfProtosProto: 3}; console.log(obj.propertyOfProtosProto);// 3
This chain is called a prototype chain. (0).__proto__ === Number.prototype && false.__proto__ === Boolean.prototype && "string".__proto__ === String.prototype && (new Date).__proto__ === Date.prototype && (function(){}/* new Function */).__proto__ === Function.prototype
All data types are inherited from Object, which means that for example: Number.prototype.__proto__ === Object.prototype
And finally, the completion of the chain: Object.prototype.__proto__ === null
var instance = {};
instance.__proto__ = FnClass.prototype;
constructorReturns = FnClass.apply(instance, arguments);
(i.e., it performs the function FnClass, passing it to the instance as this and the arguments as an array of arguments) return constructorReturns instanceof Object ? constructorReturns : instance;
//var myObj = {__proto__: {property1_OfProto: 1}} var myObj = Object.create({property1_OfProto: 1});
//myObj.__proto__.property2_OfProto = 2 Object.getPrototypeOf(myObj).property2_OfProto = 2;
if(!Object.create){ Object.create = function(proto){ var Fn = function(){}; Fn.prototype = proto; return new Fn; } }
if(!Object.getPrototypeOf){ if( (new Object).__proto__ !== Object.prototype ){ // may return incorrect value if fn.prototype has been modified Function.getPrototypeOf = function(fn){ if(typeof(fn)!=='function') throw new TypeError('Function.getPrototypeOf called on non-function'); return fn.constructor.prototype; } }else{ Object.getPrototypeOf = function(obj){ return obj.__proto__; } } }
Source: https://habr.com/ru/post/140810/
All Articles