Question

Microsoft allows to set environment variables in JScript with the following syntax:

var sh = WScript.CreateObject("Wscript.Shell");
var env = sh.Environment("PROCESS");
env("TEST") = "testvalue";

I wonder about the third line - and with me JSLint, which calls this line a "Bad Assigment".

But it works!

Is it ECMAscript standard compatible to have a function's return value as an lvalue (like here)?

If yes: How would one write such a function?

Was it helpful?

Solution

Yes, the standard permits functions to returns references. No, you cannot write such a function in Javascript. ;)

<...> the left-hand operand of an assignment is expected to produce a reference. <...> function calls are permitted to return references. This possibility is admitted purely for the sake of host objects. No built-in ECMAScript function defined by this specification returns a reference and there is no provision for a user-defined function to return a reference. http://es5.github.io/#x8.7

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top