Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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