Java Reference
In-Depth Information
Here's an example of how the Turtle example from Chapter 12 would look:
var Turtle = function(name) {
this.name = name;
this.sayHi = function() {
return "Hi dude, my name is " + this.name;
}
}
Turtle.prototype.attack = function(){
return this.name + " hits you with his " + this.weapon;
}
Harmony will introduce a class definition that would look like the following:
class Turtle {
constructor(name) {
this.name = name;
}
sayHi() {
return "Hi dude, my name is " + this.name;
}
attack() {
return this.name + " hits you with his " + this.weapon;
}
}
Inside the class definition, there's a constructor method where all the initialization code
goes. Instead of assigning methods to the prototype object, they can simply be listed inside
the class definition. The advantage here is that all the code is kept in one place, rather than
having to use a separate prototype object.
Inheritance will also be supported using the extends keyword. Back in Chapter 12, we
created a Superhuman object that inherited all the properties from the Human object.
This can be achieved in Harmony using the following code:
Search WWH ::




Custom Search