Therefore lines 25 will be creating and setting new properties of this

Therefore lines 25 will be creating and setting new

This preview shows page 214 - 216 out of 517 pages.

window. Therefore, lines 2–5 will be creating and setting new properties of this object—clearly not what we intended, but unfortunately, when this is used in a scope where it would otherwise be undefined, it refers to the global object, a serious design defect in the language. (See Fallacies and Pitfalls and To Learn More if you want to learn about the reasons for this odd behavior, a discussion of which is beyond the scope of this introduction to the language.) Second, since Movie doesn’t explicitly return anything, its return value (and therefore the value of juno ) will be undefined . Whereas a Ruby function returns the value of the last expression in the function by default, a JavaScript function returns undefined unless it has an explicit return statement. (The return in line 6 belongs to the full_title function, not to Movie itself.) Hence, lines 19–20 give errors because we’re trying to reference a property ( title ) on something that isn’t even an object. You can avoid this pitfall by rigorously following the widespread JavaScript convention that a function’s name should be capitalized if and only if the function is intended to be called as a constructor using new . Functions that are not “constructor-like” should be given names beginning with lowercase letters. Summary: Functions and Constructors JavaScript functions are first-class objects: they can be assigned to variables, passed to other functions, or returned from functions. Although JavaScript doesn’t have classes, one way of managing namespaces in an orderly way in
Image of page 214
JavaScript is to store functions as object properties, allowing a single object (hash) to collect a set of related functions as a class would. If the new keyword is used to call a function, this in the function body will refer to a new object whose property values can be initialized in the “constructor.” This mechanism is similar to creating new instances of a class, though JavaScript lacks classes. However, if a function is called without the new keyword, this in the function body will refer to the global object, which is almost never what you wanted, and the function will return undefined . To avoid this pitfall, capitalize the names of constructor-like functions intended to be called with new , but don’t capitalize the names of any other functions. ELABORATION: Prototypal inheritance Every JavaScript object inherits from exactly one prototype object—new strings inherit from String.prototype , new arrays from Array.prototype , and so on, up to Object (the empty object). If you look up a property on an object that doesn’t have that property, its prototype is checked, then its prototype’s prototype, and so on until one of the prototypes responds with the property or undefined is returned. Given this background, the effect of calling a function using the new keyword is to create a new object whose prototype is the same as the function’s prototype. Prototypes come from Self, a language originally designed at the legendary Xerox PARC and which heavily influenced NewtonScript , the programming language for the ill-fated Apple Newton “handheld.” Proper use of prototypal inheritance affords
Image of page 215
Image of page 216

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