Review of Javascript prototype inheritance. You also need to know
Object.create
and Object.getPrototypeOf
.
// Explain what the following Javascript does, what will
// be printed, and why.
var myobject = {
x: 5,
m: function (y) {
document.write(" I am here <br />");
return this.x + y;
}
};
document.write ("x = " + myobject.x + "<br />");
document.write ("m call: " + myobject.m(5) + "<br />");
var another = {
z: "hi",
m1: function() {
return this.z + " there!";
}
};
document.write ("m1 call: " + another.m1() + "<br />");
// setting the object prototype after it's been created:
another.__proto__ = myobject;
document.write ("m call: " + another.m(5) + "<br />");
another['m2'] = function () {return this.z + " " + this.x};
document.write ("m2 call: " + another.m2() + "<br />");