Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // https://i.stack.imgur.com/KFzI3.png
- // var/let/() assignments create reference to current object
- // __proto__ links to the prototype that created this current object/function. First parent in the prototype chain to make up inheritance.
- // prototype links to the object with constructor that's used new instances based on the current object
- // Instance/Object/Object Literal - Created by "Object" class. Lacks it's own prototype object for creating new objects.
- // Class/Functional Object - Created by "Function" class. Is a full object on its own, but can create object instances based on a prototype constructor that does not share local properties with this object
- // Arrow Functions - Created by "Function" class. Lacks it's own prototype object for creating new objects. Lacks arguments object.
- var FunObj = function FunObj() { var privateVar = true; if(!this.publicVar){this.publicVar = 1;} else {this.publicVar += 1;} };
- FunObj.__proto__.localVar = true;
- FunObj.localProperty = true;
- console.info('This is the referenced object in this scope that contains references to the "created by" object and prototype/definition object', FunObj);
- console.log('FunObj.publicVar', FunObj.publicVar);
- console.log('FunObj.localVar', FunObj.localVar);
- console.log('FunObj.localProperty', FunObj.localProperty);
- console.log('FunObj.__proto__', FunObj.__proto__);
- console.log('FunObj.__proto__.constructor', FunObj.__proto__.constructor);
- console.log('FunObj.__proto__.constructor.name', FunObj.__proto__.constructor.name);
- console.log('FunObj.__proto__.localProperty', FunObj.__proto__.localProperty);
- console.log('FunObj.prototype', FunObj.prototype);
- console.log('FunObj.prototype.constructor', FunObj.prototype.constructor);
- console.log('FunObj.prototype.constructor.name', FunObj.prototype.constructor.name);
- console.log('FunObj.prototype.localProperty', FunObj.prototype.localProperty);
- console.log('FunObj.prototype.publicVar', FunObj.prototype.publicVar);
- // ---
- var dataObj = new FunObj();
- console.info('This is the referenced object in this scope that contains references to the "created by" object, but has no prototype for creating more of itself. A reference to the constructor function that created this object can be found in the "created by" objects constructor property.', dataObj);
- console.log('dataObj.publicVar', dataObj.publicVar);
- console.log('dataObj.localVar', dataObj.localVar);
- console.log('dataObj.localProperty', dataObj.localProperty);
- console.log('dataObj.__proto__', dataObj.__proto__);
- console.log('dataObj.__proto__.constructor', dataObj.__proto__.constructor);
- console.log('dataObj.__proto__.constructor.name', dataObj.__proto__.constructor.name);
- console.log('dataObj.prototype', dataObj.prototype);
- // ---
- var dataCopy = new dataObj.__proto__.constructor();
- console.info('This uses the constructor function from the "created by" object to create a new instance object.', dataCopy);
- console.log('dataCopy.publicVar', dataCopy.publicVar);
- console.log('dataCopy.localVar', dataCopy.localVar);
- console.log('dataCopy.localProperty', dataCopy.localProperty);
- console.log('dataCopy.__proto__', dataCopy.__proto__);
- console.log('dataCopy.__proto__.constructor', dataCopy.__proto__.constructor);
- console.log('dataCopy.__proto__.constructor.name', dataCopy.__proto__.constructor.name);
- console.log('dataCopy.prototype', dataCopy.prototype);
- // ---
- var arrowFun = () => { var privateVariable; try{this.error = true;}catch(e){return e;} return typeof this; }
- console.info('Arrow functions cannot be invoked to create new instance objects. They also have no "arguments" object within them, so they are very limited.', arrowFun);
- console.log('arrowFun.__proto__', arrowFun.__proto__);
- console.log('arrowFun.__proto__.constructor', arrowFun.__proto__.constructor);
- console.log('arrowFun.__proto__.constructor.name', arrowFun.__proto__.constructor.name);
- console.log('arrowFun.prototype', arrowFun.prototype);
- console.log('arrowFun()', arrowFun());
- // ---
- var BlankFun = (function(){ return arguments[0] }( function(){var privateVar = true;} ));
- console.info('Functional objects created without any reference to a name will have constructors, but the name field ends up "" (blank).', BlankFun);
- console.log('BlankFun.__proto__', BlankFun.__proto__);
- console.log('BlankFun.__proto__.constructor', BlankFun.__proto__.constructor);
- console.log('BlankFun.__proto__.constructor.name', BlankFun.__proto__.constructor.name);
- console.log('BlankFun.prototype', BlankFun.prototype);
- console.log('BlankFun.prototype.constructor', BlankFun.prototype.constructor);
- console.log('BlankFun.prototype.constructor.name', BlankFun.prototype.constructor.name);
- // ---
- var AnonFun = Function('var privateVar = true; if(this!==window){this.publicMethod = function(){ return privateVar; };} return this;');
- console.info('Functions created by a "Function" object (Native code in javascript runtime) will create a constructor function, but it will be named "anonymous", which is weird.', AnonFun);
- console.log('AnonFun()', AnonFun()); // returns "Window" expectedly because this is not a data object yet.
- console.log('AnonFun.__proto__', AnonFun.__proto__);
- console.log('AnonFun.__proto__.constructor', AnonFun.__proto__.constructor);
- console.log('AnonFun.__proto__.constructor.name', AnonFun.__proto__.constructor.name); // data obj constructor is "Function"
- console.log('AnonFun.prototype', AnonFun.prototype);
- console.log('AnonFun.prototype.constructor', AnonFun.prototype.constructor);
- console.warn('AnonFun.prototype.constructor.name', AnonFun.prototype.constructor.name); // constructor's name is "anonymous"
- console.log('AnonFun.publicMethod', AnonFun.publicMethod); // expectedly not accessible
- // ---
- var af = new AnonFun();
- console.info('"Function" constructors expectedly create a functional object which can be used to create data instances objects.', af);
- console.log('af.__proto__', af.__proto__);
- console.log('af.__proto__.constructor', af.__proto__.constructor);
- console.log('af.__proto__.constructor.name', af.__proto__.constructor.name);
- console.log('af.prototype', af.prototype);
- console.warn('af.publicMethod()', af.publicMethod());
- // ---
- class ClassFun {
- constructor(){ var privateVar = true; this.privilegedMethod = function(){ return privateVar; } }
- publicMethod(){ return this; }
- static localMethod(){ return this; }
- }
- console.log('ClassFun.__proto__', ClassFun.__proto__);
- console.log('ClassFun.__proto__.constructor', ClassFun.__proto__.constructor);
- console.log('ClassFun.__proto__.constructor.name', ClassFun.__proto__.constructor.name);
- console.log('ClassFun.prototype', ClassFun.prototype);
- console.log('ClassFun.prototype.constructor', ClassFun.prototype.constructor);
- console.log('ClassFun.prototype.constructor.name', ClassFun.prototype.constructor.name);
- console.warn('ClassFun()', (function(){ try { ClassFun(); }catch(e){ return e.message; }}()));
- console.log('ClassFun.localMethod()', ClassFun.localMethod());
- console.warn('ClassFun.publicMethod()', (function(){ try { ClassFun.publicMethod(); }catch(e){ return e.message; }}()));
- // ---
- var cf = new ClassFun();
- console.log('cf.__proto__', cf.__proto__);
- console.log('cf.__proto__.constructor', cf.__proto__.constructor);
- console.log('cf.__proto__.constructor.name', cf.__proto__.constructor.name);
- console.log('cf.prototype', cf.prototype);
- console.log('cf.localMethod()', (function(){ try { cf.localMethod(); }catch(e){ return e.message; }}()));
- console.log('cf.publicMethod()',cf.publicMethod());
- // ---
- class ChildClassFun extends ClassFun {
- accessParentPublicMethod(){ return this.privilegedMethod(); }
- accessParentPrivateProperty(){ return typeof privateVar; }
- }
- console.log('ChildClassFun.__proto__', ChildClassFun.__proto__);
- console.log('ChildClassFun.__proto__.constructor', ChildClassFun.__proto__.constructor);
- console.log('ChildClassFun.__proto__.constructor.name', ChildClassFun.__proto__.constructor.name);
- console.log('ChildClassFun.prototype', ChildClassFun.prototype);
- console.log('ChildClassFun.prototype.constructor', ChildClassFun.prototype.constructor);
- console.log('ChildClassFun.prototype.constructor.name', ChildClassFun.prototype.constructor.name);
- console.warn('ChildClassFun()', (function(){ try { ChildClassFun(); }catch(e){ return e.message; }}()));
- console.log('ChildClassFun.localMethod()', ChildClassFun.localMethod());
- console.warn('ChildClassFun.publicMethod()', (function(){ try { ChildClassFun.publicMethod(); }catch(e){ return e.message; }}()));
- console.warn('ChildClassFun.accessParentPublicMethod()', (function(){ try { ChildClassFun.accessParentPublicMethod(); }catch(e){ return e.message; }}()));
- // ---
- var ccf = new ChildClassFun();
- console.log('ccf.__proto__', ccf.__proto__);
- console.log('ccf.__proto__.constructor', ccf.__proto__.constructor);
- console.log('ccf.__proto__.constructor.name', ccf.__proto__.constructor.name);
- console.log('ccf.prototype', ccf.prototype);
- console.log('ccf.localMethod()', (function(){ try { ccf.localMethod(); }catch(e){ return e.message; }}()));
- console.log('ccf.publicMethod()', ccf.publicMethod());
- console.log('ccf.accessParentPublicMethod()', ccf.accessParentPublicMethod()); // expectedly, accessor methods have no issues
- console.log('ccf.accessParentPrivateProperty()', ccf.accessParentPrivateProperty()); // expectedly, private parent class variables are not accessible to child objects
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement