If we call the Movie function using JavaScripts new keyword line 13 the value

If we call the movie function using javascripts new

This preview shows page 213 - 215 out of 517 pages.

If we call the Movie function using JavaScript’s new keyword (line 13), the value of this in the function body will be a new JavaScript object that will eventually be returned by the function, similar to Ruby’s self inside an initialize constructor method. In this case, the returned object will have properties title , year , rating , and full_title , the last of which is a property whose value is a function. If line 14 looks like a function call to you, then you’ve been hanging around Ruby too long; since functions are first-class objects in JavaScript, this line just returns the value of full_title , which is the function itself, not the result of calling it! To actually call it, we need to use parentheses, as in line 15. When we make that call, within the body of full_title , this will refer to the object whose property the function is, in this case pianist . Remember, though, that while these examples look just like calling a class’s constructor and calling an instance method in Ruby, JavaScript has no concept of classes or instance methods. In fact, there is nothing about a particular JavaScript function that makes it a constructor; instead, it’s the use of new when calling the function that makes it a constructor, causing it to create and return a new object. The reason this works is because of JavaScript’s prototype inheritance mechanism, which we don’t discuss further (but see the Elaboration below to learn more). Nonetheless, forgetting this subtle distinction may confuse you when you expect class-like behaviors and don’t get them. 1 var Movie = function(title,year,rating) { 2 this.title = title; 3 this.year = year; 4 this.rating = rating;
Image of page 213
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 Figure 6.6: Since functions are first-class objects, it is fine for an object to have a property whose value is a function, as full_title is. We will make extensive use of this characteristic. Note the pitfall in lines 14–18. However, a JavaScript misfeature can trip us up here. It is (unfortunately) perfectly legal to call Movie as a plain old function without using the new keyword, as in line 17. If you do this, JavaScript’s behavior is completely different in two horrible, horrible ways. First, in the body of Movie , this will not refer to a brand-new object but instead to the global object, which defines various special constants such as Infinity , NaN , and null , and supplies various other parts of the JavaScript environment. When JavaScript is run in a browser, the global object happens to be a data structure representing the browser window. Therefore, lines 2–5 will be creating and setting new properties of this object—clearly not
Image of page 214
Image of page 215

You've reached the end of your free preview.

Want to read all 517 pages?

  • Spring '19
  • Dr.Marcos

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors