Advertisement
Guest User

Object types in Javascript

a guest
Jun 28th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // https://i.stack.imgur.com/KFzI3.png
  2.  
  3. // var/let/() assignments create reference to current object
  4. // __proto__  links to the prototype that created this current object/function. First parent in the prototype chain to make up inheritance.
  5. // prototype  links to the object with constructor that's used new instances based on the current object
  6.  
  7. // Instance/Object/Object Literal - Created by "Object" class. Lacks it's own prototype object for creating new objects.
  8. // 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
  9. // Arrow Functions - Created by "Function" class. Lacks it's own prototype object for creating new objects. Lacks arguments object.
  10.  
  11. var FunObj = function FunObj() { var privateVar = true; if(!this.publicVar){this.publicVar = 1;} else {this.publicVar += 1;} };
  12. FunObj.__proto__.localVar = true;
  13. FunObj.localProperty = true;
  14. console.info('This is the referenced object in this scope that contains references to the "created by" object and prototype/definition object', FunObj);
  15. console.log('FunObj.publicVar', FunObj.publicVar);
  16. console.log('FunObj.localVar', FunObj.localVar);
  17. console.log('FunObj.localProperty', FunObj.localProperty);
  18. console.log('FunObj.__proto__', FunObj.__proto__);
  19. console.log('FunObj.__proto__.constructor', FunObj.__proto__.constructor);
  20. console.log('FunObj.__proto__.constructor.name', FunObj.__proto__.constructor.name);
  21. console.log('FunObj.__proto__.localProperty', FunObj.__proto__.localProperty);
  22. console.log('FunObj.prototype', FunObj.prototype);
  23. console.log('FunObj.prototype.constructor', FunObj.prototype.constructor);
  24. console.log('FunObj.prototype.constructor.name', FunObj.prototype.constructor.name);
  25. console.log('FunObj.prototype.localProperty', FunObj.prototype.localProperty);
  26. console.log('FunObj.prototype.publicVar', FunObj.prototype.publicVar);
  27.  
  28. // ---
  29.  
  30. var dataObj = new FunObj();
  31. 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);
  32. console.log('dataObj.publicVar', dataObj.publicVar);
  33. console.log('dataObj.localVar', dataObj.localVar);
  34. console.log('dataObj.localProperty', dataObj.localProperty);
  35. console.log('dataObj.__proto__', dataObj.__proto__);
  36. console.log('dataObj.__proto__.constructor', dataObj.__proto__.constructor);
  37. console.log('dataObj.__proto__.constructor.name', dataObj.__proto__.constructor.name);
  38. console.log('dataObj.prototype', dataObj.prototype);
  39.  
  40. // ---
  41.  
  42. var dataCopy = new dataObj.__proto__.constructor();
  43. console.info('This uses the constructor function from the "created by" object to create a new instance object.', dataCopy);
  44. console.log('dataCopy.publicVar', dataCopy.publicVar);
  45. console.log('dataCopy.localVar', dataCopy.localVar);
  46. console.log('dataCopy.localProperty', dataCopy.localProperty);
  47. console.log('dataCopy.__proto__', dataCopy.__proto__);
  48. console.log('dataCopy.__proto__.constructor', dataCopy.__proto__.constructor);
  49. console.log('dataCopy.__proto__.constructor.name', dataCopy.__proto__.constructor.name);
  50. console.log('dataCopy.prototype', dataCopy.prototype);
  51.  
  52. // ---
  53.  
  54. var arrowFun = () => { var privateVariable; try{this.error = true;}catch(e){return e;} return typeof this; }
  55. 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);
  56. console.log('arrowFun.__proto__', arrowFun.__proto__);
  57. console.log('arrowFun.__proto__.constructor', arrowFun.__proto__.constructor);
  58. console.log('arrowFun.__proto__.constructor.name', arrowFun.__proto__.constructor.name);
  59. console.log('arrowFun.prototype', arrowFun.prototype);
  60. console.log('arrowFun()', arrowFun());
  61.  
  62. // ---
  63.  
  64. var BlankFun = (function(){ return arguments[0] }( function(){var privateVar = true;} ));
  65. console.info('Functional objects created without any reference to a name will have constructors, but the name field ends up "" (blank).', BlankFun);
  66. console.log('BlankFun.__proto__', BlankFun.__proto__);
  67. console.log('BlankFun.__proto__.constructor', BlankFun.__proto__.constructor);
  68. console.log('BlankFun.__proto__.constructor.name', BlankFun.__proto__.constructor.name);
  69. console.log('BlankFun.prototype', BlankFun.prototype);
  70. console.log('BlankFun.prototype.constructor', BlankFun.prototype.constructor);
  71. console.log('BlankFun.prototype.constructor.name', BlankFun.prototype.constructor.name);
  72.  
  73. // ---
  74.  
  75. var AnonFun = Function('var privateVar = true; if(this!==window){this.publicMethod = function(){ return privateVar; };} return this;');
  76. 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);
  77. console.log('AnonFun()', AnonFun()); // returns "Window" expectedly because this is not a data object yet.
  78. console.log('AnonFun.__proto__', AnonFun.__proto__);
  79. console.log('AnonFun.__proto__.constructor', AnonFun.__proto__.constructor);
  80. console.log('AnonFun.__proto__.constructor.name', AnonFun.__proto__.constructor.name); // data obj constructor is "Function"
  81. console.log('AnonFun.prototype', AnonFun.prototype);
  82. console.log('AnonFun.prototype.constructor', AnonFun.prototype.constructor);
  83. console.warn('AnonFun.prototype.constructor.name', AnonFun.prototype.constructor.name); // constructor's name is "anonymous"
  84. console.log('AnonFun.publicMethod', AnonFun.publicMethod); // expectedly not accessible
  85.  
  86. // ---
  87.  
  88. var af = new AnonFun();
  89. console.info('"Function" constructors expectedly create a functional object which can be used to create data instances objects.', af);
  90. console.log('af.__proto__', af.__proto__);
  91. console.log('af.__proto__.constructor', af.__proto__.constructor);
  92. console.log('af.__proto__.constructor.name', af.__proto__.constructor.name);
  93. console.log('af.prototype', af.prototype);
  94. console.warn('af.publicMethod()', af.publicMethod());
  95.  
  96. // ---
  97.  
  98. class ClassFun {
  99.   constructor(){ var privateVar = true; this.privilegedMethod = function(){ return privateVar; } }
  100.   publicMethod(){ return this; }
  101.   static localMethod(){ return this; }
  102. }
  103. console.log('ClassFun.__proto__', ClassFun.__proto__);
  104. console.log('ClassFun.__proto__.constructor', ClassFun.__proto__.constructor);
  105. console.log('ClassFun.__proto__.constructor.name', ClassFun.__proto__.constructor.name);
  106. console.log('ClassFun.prototype', ClassFun.prototype);
  107. console.log('ClassFun.prototype.constructor', ClassFun.prototype.constructor);
  108. console.log('ClassFun.prototype.constructor.name', ClassFun.prototype.constructor.name);
  109. console.warn('ClassFun()', (function(){ try { ClassFun(); }catch(e){ return e.message; }}()));
  110. console.log('ClassFun.localMethod()', ClassFun.localMethod());
  111. console.warn('ClassFun.publicMethod()', (function(){ try { ClassFun.publicMethod(); }catch(e){ return e.message; }}()));
  112.  
  113. // ---
  114.  
  115. var cf = new ClassFun();
  116. console.log('cf.__proto__', cf.__proto__);
  117. console.log('cf.__proto__.constructor', cf.__proto__.constructor);
  118. console.log('cf.__proto__.constructor.name', cf.__proto__.constructor.name);
  119. console.log('cf.prototype', cf.prototype);
  120. console.log('cf.localMethod()', (function(){ try { cf.localMethod(); }catch(e){ return e.message; }}()));
  121. console.log('cf.publicMethod()',cf.publicMethod());
  122.  
  123. // ---
  124.  
  125. class ChildClassFun extends ClassFun {
  126.   accessParentPublicMethod(){ return this.privilegedMethod(); }
  127.   accessParentPrivateProperty(){ return typeof privateVar; }
  128. }
  129. console.log('ChildClassFun.__proto__', ChildClassFun.__proto__);
  130. console.log('ChildClassFun.__proto__.constructor', ChildClassFun.__proto__.constructor);
  131. console.log('ChildClassFun.__proto__.constructor.name', ChildClassFun.__proto__.constructor.name);
  132. console.log('ChildClassFun.prototype', ChildClassFun.prototype);
  133. console.log('ChildClassFun.prototype.constructor', ChildClassFun.prototype.constructor);
  134. console.log('ChildClassFun.prototype.constructor.name', ChildClassFun.prototype.constructor.name);
  135. console.warn('ChildClassFun()', (function(){ try { ChildClassFun(); }catch(e){ return e.message; }}()));
  136. console.log('ChildClassFun.localMethod()', ChildClassFun.localMethod());
  137. console.warn('ChildClassFun.publicMethod()', (function(){ try { ChildClassFun.publicMethod(); }catch(e){ return e.message; }}()));
  138. console.warn('ChildClassFun.accessParentPublicMethod()', (function(){ try { ChildClassFun.accessParentPublicMethod(); }catch(e){ return e.message; }}()));
  139.  
  140. // ---
  141.  
  142. var ccf = new ChildClassFun();
  143. console.log('ccf.__proto__', ccf.__proto__);
  144. console.log('ccf.__proto__.constructor', ccf.__proto__.constructor);
  145. console.log('ccf.__proto__.constructor.name', ccf.__proto__.constructor.name);
  146. console.log('ccf.prototype', ccf.prototype);
  147. console.log('ccf.localMethod()', (function(){ try { ccf.localMethod(); }catch(e){ return e.message; }}()));
  148. console.log('ccf.publicMethod()', ccf.publicMethod());
  149. console.log('ccf.accessParentPublicMethod()', ccf.accessParentPublicMethod()); // expectedly, accessor methods have no issues
  150. 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