Skip to content

Instantly share code, notes, and snippets.

@dandean
Created August 5, 2009 04:56
Show Gist options
  • Save dandean/162509 to your computer and use it in GitHub Desktop.
Save dandean/162509 to your computer and use it in GitHub Desktop.
MIN_VALUE / MAX_VALUE Examples
/**
* 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