5

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?

1

1 Answer 1

8

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

5
  • but what you can do is setting properties of the Object, the returned reference points to ret("sth").someProp = "sth" May 22, 2013 at 8:36
  • This was quick and exactly the answer I was looking for. Thank you.
    – rplantiko
    May 22, 2013 at 8:39
  • 1
    @C5H8NNaO4: this doesn't mean ret() returns a reference. Reference is a pair [base,prop] and the function only evaluates the base part.
    – georg
    May 22, 2013 at 8:39
  • 1
    @thg435 Yes, what i meant was sth like var someObjs = [{a:"",someProp:"b"}]; (function (a) {return someObjs[a]})(0).someProp = "c";console.log(someObjs[0].someProp) //c May 22, 2013 at 8:45
  • 1
    @C5H8NNaO4: Still, your IEFE there returns the {a:"",someProp:"b"} object on which then a property is set, it does not return a reference.
    – Bergi
    Sep 16, 2013 at 23:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.