Created
March 28, 2015 13:22
-
-
Save MarkLavrynenko/5a321b8023a87fcd51aa to your computer and use it in GitHub Desktop.
JS cool features
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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