Advertisement
KarnakGames

JavaScript object creation algorithm

Feb 10th, 2013
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Source: http://dmitrysoshnikov.com/ecmascript/chapter-7-2-oop-ecmascript-implementation/
  2. // Given:
  3. // var object = new Function(params);
  4. // Algorithm behind:
  5.  
  6. F.[[Construct]](initialParameters):
  7.  
  8. O = new NativeObject();
  9.  
  10. // property [[Class]] is set to "Object", i.e. simple object
  11. O.[[Class]] = "Object"
  12.  
  13. // get the object on which
  14. // at the moment references F.prototype
  15. var __objectPrototype = F.prototype;
  16.  
  17. // if __objectPrototype is an object, then:
  18. O.[[Prototype]] = __objectPrototype
  19. // else:
  20. O.[[Prototype]] = Object.prototype;
  21. // where O.[[Prototype]] is the prototype of the object
  22.  
  23. // initialization of the newly created object
  24. // applying the F.[[Call]]; pass:
  25. // as this value – newly created object - O,
  26. // arguments are the same as initialParameters for F
  27. R = F.[[Call]](initialParameters); this === O;
  28. // where R is the returned value of the [[Call]]
  29. // in JS view it looks like:
  30. // R = F.apply(O, initialParameters);
  31.  
  32. // if R is an object
  33. return R
  34. // else
  35. return O
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement