Created
January 25, 2012 19:30
-
-
Save jussi-kalliokoski/1678065 to your computer and use it in GitHub Desktop.
Extend Number.prototype with Math functions
This file contains hidden or 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 (define, names) { | |
if (!define || !names.forEach) return; | |
names.forEach(function (name) { | |
define(Number.prototype, name, { | |
value: function () { | |
return Math[name].apply( | |
Math, | |
[this].concat([].slice.call(arguments)) | |
); | |
}, | |
writable: true, | |
enumerable: false, | |
}); | |
}); | |
}(Object.defineProperty, [ | |
'abs', | |
'acos', | |
'asin', | |
'atan', | |
'atan2', | |
'ceil', | |
'cos', | |
'exp', | |
'floor', | |
'log', | |
'max', | |
'min', | |
'pow', | |
'round', | |
'sin', | |
'sqrt', | |
'tan' | |
])); |
This file contains hidden or 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
#!/usr/bin/env js | |
load('number-math.js'); | |
var k; | |
for (k in 4) { | |
if ((4).hasOwnProperty(k)) { | |
print('The test has failed! Reason: Number has enumerable features.'); | |
exit(1); | |
} | |
} | |
var x = 2.1; | |
print(x.sqrt()); | |
print(x.pow(2)); | |
print(x.sin()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment