Is there a shortcut for
myVar = myFunction(myVar);
?
Similar to myVar+=2; instead of myVar=myVar+2;
Thanks!
No, no such expression exists. If we examine ECMAScript 5 section 11.13, we see that there are simple assignments (=) and compound assignments (op=). The compound assignment operators are listed exhaustively in the spec:
*= /= %= += -= <<= >>= >>>= &= ^= |=
None of these operators apply a named function.
Well, there's no specific operator, however nothing prevents you from doing something similar using an object as a variable context.
function VarContext() {}
VarContext.prototype.someFunction = function (member) {
//could be any kind of processing
this[member] += 1;
};
var ctx = new VarContext();
ctx.test = 1;
ctx.someFunction('test');
ctx.test; //2
Functions can be bound to the object as well.
someFunction = ctx.someFunction.bind(ctx);
Then you can just write:
someFunction('test');
functionmyFunctiondo?myVar = myFunction(myVar);means assigning value tomyvarafter functionreturnsthe value. What you want to do actually?