Java Reference
In-Depth Information
Superhuman.init = function(name, realName){
this.name = name;
this.realName = realName;
this.init = undefined; // this line removes the init
function, so
it can only be called once
return this;
}
Now a new object can easily be created and initialized:
batman = Object.create(Superhuman);
batman.init("Batman","Bruce Wayne");
batman.name;
<< "Batman"
batman.realName;
<< "Bruce Wayne"
A new object can also be created and initialized in a single line using chaining (a technique
that will be explained in more detail later in the chapter):
aquaman = Object.create(Superhuman).init("Aquaman",
"Arthur Curry");
<< "arms": 2, "change": function () {
return this.realName + " goes into a phone box and
comes out as
" + this.name;
}, "init": undefined, "legs": 2, "name": "Aquaman",
"realName":
"Arthur Curry", "walk": function () {
console.log("Walking"); }}
Object Prototype Chain
Creating objects from objects will create a prototype chain.
Search WWH ::




Custom Search