Can a function's return value be an lvalue in JavaScript?

Deadly 提交于 2019-12-23 13:09:03

问题


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:


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



来源:https://stackoverflow.com/questions/16686974/can-a-functions-return-value-be-an-lvalue-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!