In javascript when I create this function it does what can be expected :
Number.prototype.powown = function(b) {
return Math.pow(this, b);
}
var a = 3;
var b = 6;
document.write(a.powown(b));
But I want to have it working without the use of variables, I can't figure out how this works.
I would like to make it work when I give this code :
document.write(Math.powown(3,6));
since Math
object already have Math.pow() you could directly use it, in case you want to add custom functions , you could add custom function to Math object, for example:
Math.powown = function() {
return Math.pow(arguments[0], arguments[1]);
};
console.log(Math.powown(2,2)); //gives 4
I think what you are trying to do is this
Number.prototype.pow = function(a){ return Math.pow(this, a); }
So you can then call it like this
(10).pow(5);
or this
10..pow(5);
Note: You wont be able to call 10.pow(5)
the parenthesis or . are required for javascript to know 10 is Number
Bonus: If you wanted to get really silly you could apply all the methods of Math to the prototype of Number like this.
Object.getOwnPropertyNames(Math).forEach(function(p){
Number.prototype[p] = function(){
return Math[p].apply(null, Array.prototype.concat.apply([this], arguments));
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.