Java Reference
In-Depth Information
↵
+ this.name;
}
This method relies on the
name
and
realName
properties. It can be a good idea to create
default values in the prototype so that the method will still work. In this case, we can use
names that prompt some real data to be added:
Superhuman.name = "Name Needed";
<< "Name Needed"
Superhuman.realName = "Real Name Needed";
<< "Real Name Needed"
Now we can use the
Superhuman
object as a prototype to create lots of unique
Super-
human
objects:
superman = Object.create(Superhuman);
<< {"arms": 2, "change": function () {
return this.realName + " goes into a phone box and
comes out
↵
as " + this.name;
}, "legs": 2, "name": "Name Needed", "realName": "Real
Name
↵
Needed", "walk": function () { console.log("Walking"); }}
Once a
Superhuman
object has been created, we can add specific properties by assign-
ment:
superman.name = "Superman";
superman.realName = "Clarke Kent";
This method of adding custom properties is certainly more long-winded than using a con-
structor function, where the initial values are passed as an argument to the constructor func-
tion. This can be fixed by adding an
init()
method to the
Superhuman
object that ac-
cepts initialization properties:
