HTML and CSS Reference
In-Depth Information
Correct answer:
B
2.
Incorrect:
The global namespace isn't reserved for the browser.
A.
Correct:
Because
the global namespace is available to all applications in the
session, using it could result in a naming conflict.
B.
Incorrect:
The global namespace doesn't create a security risk to the user's system.
C.
Correct answer:
C
3.
Incorrect:
The
it
keyword doesn't exist.
A.
Incorrect:
The
document.current
keyword doesn't provide the reference.
B.
Correct:
The
this
keyword provides a reference to the object.
C.
Incorrect:
The
this
keyword provides a direct shortcut to the element that raised
the event.
D.
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;
document.write(x.sum());
Search WWH ::
Custom Search