alankis

Public property instantiation JS

Oct 24th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // class product
  2. var Product = function(product) {
  3.     //this.title = product.title;
  4.     //this.price = product.price;
  5.    this.id = Object.getPrototypeOf(this).id++;
  6. };
  7.  
  8. // static member - shared between object constructor and inherited in instances
  9. Product.prototype.id = 0;
  10.  
  11. // nethod on class = static method
  12.  
  13. Product.unique_id = function() {
  14.     return Product.prototype.id++;
  15. };
  16.  
  17. /* Simple test methods both on instance and static */
  18.  
  19. var p1 = new Product();
  20. var p2 = new Product();
  21. console.log(p1.id);
  22. console.log(p2.id);
  23.  
  24. console.log("Call 1: " + Product.unique_id() + "// 2");
  25. console.log("Call 2: " + Product.unique_id() + "// 3");
  26. console.log("Call 3: " + Product.unique_id() + "// 4");
  27.  
  28. /* end of tests */
  29.  
  30. var p1 = new Product({
  31.     title: 'Keyboard',
  32.     price: 10500
  33. });
  34.  
  35. //p1.id(); // returns it's id
Advertisement
Add Comment
Please, Sign In to add comment