Advertisement
saasbook

prototype_example.js

Aug 15th, 2013
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. var Movie = function(title,year,rating) {
  2. this.title = title;
  3. this.year = year;
  4. this.rating = rating;
  5. this.full_title = function() { // "instance method"
  6. return(this.title + ' (' + this.year + ')');
  7. };
  8. };
  9. function Movie(title,year,rating) { // this syntax may look familiar...
  10. // ...
  11. }
  12. // using 'new' makes Movie the new objects' prototype:
  13. pianist = new Movie('The Pianist', 2002, 'R');
  14. pianist.full_title; // => function() {...}
  15. pianist.full_title(); // => "The Pianist (2002)"
  16. // BAD: without 'new', 'this' is bound to global object in Movie call!!
  17. juno = Movie('Juno', 2007, 'PG-13'); // DON'T DO THIS!!
  18. juno; // undefined
  19. juno.title; // error: 'undefined' has no properties
  20. juno.full_title(); // error: 'undefined' has no properties
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement