Advertisement
Guest User

instantiateing prototype and __proto__ parent class

a guest
Oct 2nd, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1.  
  2. function Class(){}
  3. var instance1 = new Class();
  4.  
  5. var instance2 = {};
  6.  
  7. instance2.constructor = Class;
  8. instance2.__proto__ = Class.prototype;
  9.  
  10. //rototype when instantiate, you already set prototype's ___proto__ to the function's constructor
  11.  
  12. Class.apply(instance2);
  13.  
  14. instance[1|2].constructor == Class
  15. instance[1|2].__proto__ = Class.prototype
  16. instance[1|2] instanceof Class = true
  17.  
  18. //What does a function's prototype do?
  19.  
  20. function Circle(radius) {
  21. this.radius = radius;
  22. }
  23.  
  24. Circle.prototype.area = function() {
  25. return this.radius * this.radius * Math.Pi;
  26. }
  27.  
  28. var instance = new Circle(5);
  29. instance.area() ==> 78.5275
  30.  
  31. instance.constructor == Circle
  32. instance.__proto__ == Circle.prototype
  33. instance instanceof Circle = true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement