0

Is there a shortcut for

myVar = myFunction(myVar);

?

Similar to myVar+=2; instead of myVar=myVar+2;

Thanks!

3
  • It depends on, what transformation, does the function myFunction do? Commented Jul 20, 2014 at 18:20
  • Get the concepts clearly. myVar = myFunction(myVar); means assigning value to myvar after function returns the value. What you want to do actually? Commented Jul 20, 2014 at 18:21
  • No, there is no such shortcut. If you want to mutate objects though, you might use a method call for this. Commented Jul 20, 2014 at 19:02

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.