Java Reference
In-Depth Information
lois.job = "Reporter";
<< "Reporter"
An alternative way is to add a second argument to the
Object.create()
method con-
taining properties that are to be added to the new object:
jimmy = Object.create(Human, { name: { value: "Jimmy
Olsen",
↵
enumerable: true }, job: { value: "Photographer",
↵
enumerable: true } });
<< {"arms": 2, "job": "Photographer", "legs": 2, "name":
"Jimmy
↵
Olsen", "walk": function () { console.log("Walking"); }}
This method is a little unwieldy as the properties have to be added using property
descriptors, making the syntax awkward and overall verbose. It's often easier to create the
object and then add each new property one by one. This can be made quicker using the
mixin()
method that is covered later.
Object-based Inheritance
The
Human
object can also act like a “super-class” and become the prototype of another
prototype object called
Superhuman
. This will have all the properties and methods that
the
Human
object has, but with some extra methods:
Superhuman = Object.create(Human);
<< {"arms": 2, "legs": 2, "walk": function ()
↵
{ console.log("Walking"); }}
Superhuman.change = function() {
return this.realName + " goes into a phone box and comes
out as "
↵
+ this.name;
}
<< function () {
return this.realName + " goes into a phone box and comes
out as "
