Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var Movie = function(title,year,rating) {
- this.title = title;
- this.year = year;
- this.rating = rating;
- this.full_title = function() { // "instance method"
- return(this.title + ' (' + this.year + ')');
- };
- };
- function Movie(title,year,rating) { // this syntax may look familiar...
- // ...
- }
- // using 'new' makes Movie the new objects' prototype:
- pianist = new Movie('The Pianist', 2002, 'R');
- pianist.full_title; // => function() {...}
- pianist.full_title(); // => "The Pianist (2002)"
- // BAD: without 'new', 'this' is bound to global object in Movie call!!
- juno = Movie('Juno', 2007, 'PG-13'); // DON'T DO THIS!!
- juno; // undefined
- juno.title; // error: 'undefined' has no properties
- juno.full_title(); // error: 'undefined' has no properties
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement