HTML and CSS Reference
In-Depth Information
Correct answer: B
Incorrect: The global namespace isn't reserved for the browser.
Correct: Because the global namespace is available to all applications in the
session, using it could result in a naming conflict.
Incorrect: The global namespace doesn't create a security risk to the user's system.
Correct answer: C
Incorrect: The it keyword doesn't exist.
Incorrect: The document.current keyword doesn't provide the reference.
Correct: The this keyword provides a reference to the object.
Incorrect: The this keyword provides a direct shortcut to the element that raised
the event.
Objective 1.6: Thought experiment
To extend a native object, you must add functionality to its prototype. This isn't ideal object
orientation in all cases; however, it does provide another avenue to achieve the desired
results. JavaScript objects are dynamic, meaning that the prototype can be modified to add
functionality to them. In this example, you extend the Array object to provide a sum method
that returns the sum of all the elements in the array.
For this type of example, type safety is a concern. Arrays can hold any type of data. True
inheritance would allow you to create a special type of array to hold only numbers. In this
code, you can add validation to ensure that the values in the array are numeric or add only
the numeric values. However, for the sake of demonstrating the extension of the object, this
sample omits that validation. The following code shows how you can extend the Array object
to support a sum method by adding a new method to its prototype:
Array.prototype.sum = function () {
var res = 0;
for (var i = 0; i < this.length; i++)
res += this[i];
return res;
var x = new Array(2);
x[0] = 5;
x[1] = 6;
Search WWH ::

Custom Search