Proper use of prototypal inheritance aff an effective kind of implementation

Proper use of prototypal inheritance aff an effective

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

, the programming language for the ill-fated Apple Newton “handheld.” Proper use of prototypal inheritance affords an effective kind of implementation reuse that is different from what classes provide. Unfortunately, as Crockford notes in JavaScript: The Good Parts Crockford 2008 , JavaScript’s implementation of prototypal inheritance is halfhearted and uses a confusing syntax, perhaps in an effort to resemble “classical” languages with class inheritance. Self-Check 6.3.1. What is the difference between evaluating square.area and square.area() in the following JavaScript code? 1 var square = { 2 side: 3, 3 area: function() { 4 return this.side*this.side; 5 } 6 }; square.area() is a function call that in this case will return 9, whereas square.area is an unapplied function object. Self-Check 6.3.2. Given the code in Self-Check 6.3.1 , explain why it’s is incorrect to write s=new square . square is just an object, not a function, so it cannot be called as a constructor (or at all). 6.4 The Document Object Model and jQuery The World Wide Web Consortium Document Object Model (W3C DOM) is “a platform- and language- neutral interface that will allow programs and scripts to dynamically access and update the content,
Image of page 215
structure and style of documents”—in other words, a standard representation of an HTML, XML, or XHTML document consisting of a hierarchy of elements. A DOM element is recursively defined in that one of its properties is an array of child elements, as Figure 6.7 shows. Hence a DOM node representing the <html> element of an HTML page is sufficient to represent the whole page, since every element on a well-formed page is a descendant of <html> . Other DOM element properties correspond to the HTML element’s attributes ( href , src , and so on). When a browser loads a page, the HTML of the page is parsed into a DOM tree similar to Figure 6.7 . DOM technically refers to the standard itself, but developers often use it to mean the specific DOM tree corresponding to the current page. Figure 6.7: A simplified view of the DOM tree corresponding to the RottenPotatoes “list of movies” page with skeletal HTML markup. An open triangle indicates places where we’ve elided the rest of the subtree for brevity. this.document is set to point to the DOM tree’s root when a page is loaded. How does JavaScript get access to the DOM? When JavaScript is embedded in a browser, the global object, named by the global variable window , defines additional browser-specific properties and functions, collectively called the JSAPI. Whenever a new page is loaded, a new global window object is created that shares no data with the global objects of other visible pages. One of the properties of the global object is window.document , which is the root element of the current document’s DOM tree and also defines some functions to query, traverse, and modify the DOM; one of the most common is getElementById , which you may have run across while perusing others’ JavaScript code. However, to avoid compatibility problems stemming from different browsers’ implementations of the
Image of page 216
Image of page 217

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