I get the following error ReferenceError: double is not defined
Code:
Number.prototype.double = function (){
return this*2;
}
x=[1,2]
console.log(x.map(double));
how can i fix it?
I get the following error ReferenceError: double is not defined
Code:
Number.prototype.double = function (){
return this*2;
}
x=[1,2]
console.log(x.map(double));
how can i fix it?
You can't do that like that. The reason is that this inside of map is not a number. That's why you cannot do it with a prototype like you wish.
What you can do is the following: get the passed parameter from map
Number.double = function (e){
return e*2;
}
x=[1,2]
console.log(x.map(Number.double));
EDIT: if you really need the prototype solution, you can do the following:
Number.prototype.double = function (e){
if (e) return e*2;
else return this*2;
}
x=[1,2]
console.log(x.map(Number.prototype.double));
double() is now no longer a method of Number - this has changed the code completely. You can no longer say n.double() to double a number.static method instead of member one.