Skip to content

Instantly share code, notes, and snippets.

@MarkLavrynenko
Created March 28, 2015 13:22
Show Gist options
  • Save MarkLavrynenko/5a321b8023a87fcd51aa to your computer and use it in GitHub Desktop.
Save MarkLavrynenko/5a321b8023a87fcd51aa to your computer and use it in GitHub Desktop.
JS cool features
function SpaceShip(pilot, maxSpeed) {
this.pilot = pilot;
this.maxSpeed = maxSpeed;
}
SpaceShip.UPDATE_PILOT = "updatePilot";
SpaceShip.prototype.updatePilot = function (newAge, newSkill) {
var notifier = Object.getNotifier(this);
var self = this;
notifier.performChange(SpaceShip.UPDATE_PILOT, function() {
self.pilot.age = newAge;
self.pilot.skill = newSkill;
}, this);
notifier.notify({
object : self,
type : SpaceShip.UPDATE_PILOT,
newPilot : self.pilot
});
}
var ship = new SpaceShip({
name : "Mark",
age : 20,
skill : 50
}, 792);
Object.observe(ship, function(updates){
console.log("Ship updated ", updates)
}, [SpaceShip.UPDATE_PILOT])
ship.updatePilot(21, 75);
console.log("Ship in the end ", ship)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment