Let's say i want an object that when multiplied with, it multiplies by two and subtracts 1. The syntax would look like this:
var a = {
on_multiply: function(context){
return context*2-1
}
};
alert(2*a);
This would output 3. I don't want to write "a.on_multiply(2)" Is there a way to do this? If yes, is it possible to do this with arrays or matrixes also?
The simplest way I can think of to make the above example work is to assign a function named a, and have the context as a parameter of that function:
function a(context) {
return (context * 2) - 1;
}
And if you really wanted a to be a function assigned to a name:
const a = context => 2 * context - 1;
And the above in ES5 syntax:
const a = functipn(context) {
return (context * 2) - 1;
}
Hopefully this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With