Skip to content

Instantly share code, notes, and snippets.

@jussi-kalliokoski
Created January 25, 2012 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jussi-kalliokoski/1678065 to your computer and use it in GitHub Desktop.
Save jussi-kalliokoski/1678065 to your computer and use it in GitHub Desktop.
Extend Number.prototype with Math functions
(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'
]));
#!/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