Created
August 5, 2009 04:56
-
-
Save dandean/162509 to your computer and use it in GitHub Desktop.
MIN_VALUE / MAX_VALUE Examples
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
/** | |
* class Person | |
* | |
* An example use case for Number.MIN_VALUE and Number.MAX_VALUE. | |
**/ | |
var Person = Class.create((function() { | |
var _age = Number.MIN_VALUE, _name; | |
return { | |
/** | |
* new Person(name[, age]); | |
**/ | |
initialize: function(name, age) { | |
_name = name; | |
if (!Object.isUndefined(age)) { | |
_age = age; // Use given age, if found. | |
} | |
}, | |
speak: function(message) { | |
console.log(_name + ": " + message); | |
return this; | |
}, | |
speakYourAge: function() { | |
var msg = ""; | |
switch(true) { | |
case _age == Number.MIN_VALUE: | |
msg = "I'm not born yet."; break; | |
case _age == Number.MAX_VALUE: | |
msg = "I'm dead."; break; | |
default: msg = "I am #{years} years old.".interpolate({years: _age}); | |
} | |
this.speak(msg); | |
return this; | |
}, | |
live: function() { | |
_age = 0; | |
this.speak("Huzzah! I'm alive!"); | |
return this; | |
}, | |
age: function() { | |
if (_age == Number.MIN_VALUE) { | |
this.speak("I can't age. I've not been born yet.") | |
} else if (_age == Number.MAX_VALUE) { | |
this.speak("I can't age. I'm dead.") | |
} else { | |
_age++; | |
if (_age == Number.MAX_VALUE) this.die(); | |
this.speakYourAge(); | |
} | |
return this; | |
}, | |
die: function() { | |
_age = Number.MAX_VALUE; | |
this.speak("Gack! I'm dying!"); | |
return this; | |
}, | |
isAlive: function() { | |
var alive = _age != Number.MIN_VALUE && _age != Number.MAX_VALUE; | |
this.speak((alive) ? "Yes, I am alive." : "No, I am not alive."); | |
return alive; | |
}, | |
toString: function() { return "[object Person]"; } | |
} | |
})()); | |
// Create Alex | |
var alex = new Person("Alex"); | |
// Check if he's alive. | |
alex.isAlive(); | |
// Live, age, etc | |
alex.speakYourAge() | |
.live().speakYourAge() | |
.age().age().age().isAlive(); | |
// Die, then check if he's alive. | |
alex.die().speakYourAge().isAlive(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment