Advertisement
saasbook

Untitled

Mar 14th, 2012
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // "class" Square (capitalize by convention)
  2. function Square(side_length)  {
  3.   this.side = side_length;    // "instance variable"
  4.   this.area = function() {    // "instance method"
  5.      return this.side*this.side;
  6.   }
  7. }
  8. // "instance"
  9. var someSquare = new Square(3);
  10.  
  11. // better, since keeps instance methods separate and avoids having N copies of them
  12. function Square(side_length) {
  13.   this.side = side_length;
  14. }
  15. // .prototype is a special property defined only on functions.  The 'new'
  16. // constructor sets __proto__ property of new object from .prototype's value.
  17. // Also, __proto__ is not writable in IE's implementation of JScript.
  18. Square.prototype.area = function() {
  19.   return this.side * this.side;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement