Created
May 18, 2011 23:05
-
-
Save gabehollombe/979788 to your computer and use it in GitHub Desktop.
Adding a custom 'seconds' function on to Number in JavaScript
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
> Number.prototype.seconds = function(){ return this * 1000 } | |
=> function (){ return this * 1000 } | |
> 1..seconds() | |
=> 1000 | |
> (1).seconds() | |
=> 1000 | |
> 1['seconds']() | |
=> 1000 |
Author
gabehollombe
commented
May 22, 2011
via email
Very nice. =-)
…On Sun, May 22, 2011 at 9:16 PM, xachro < ***@***.***>wrote:
//time to get funky
function Days(num){
this.num = num;
}
Days.prototype = {
get ago(){
return new Date((new Date()).getTime() - (this.num_24_60_60_1000));
},
get from_now(){
return new Date((new Date()).getTime() + (this.num_24_60_60_1000));
}
}
Number.prototype.**defineGetter**("days", function(){return new
Days(this)});
Number.prototype.**defineGetter**("day", function(){return new
Days(this)});
> 1..day.ago
> => Sat May 21 2011 21:15:19 GMT+1000 (EST)
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/979788
Pity some "browsers" don't support getters and setters.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment