Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 747
rplantiko Avatar asked May 22 '13 08:05

rplantiko


People also ask

What type of value can a function return in JavaScript?

JavaScript functions can return a single value. To return multiple values from a function, you can pack the return values as elements of an array or as properties of an object.

What is Lvalue in JavaScript?

An lvalue refers to an object that persists beyond a single expression. An rvalue is a temporary value that does not persist beyond the expression that uses it.

Should a function always return a value JavaScript?

It's a ground rule. JavaScript follows this rule, functions always return something. In the case where you don't want to return a value, something else exists.

What happens when you return something in JavaScript?

When a return statement is used in a function body, the execution of the function is stopped. If specified, a given value is returned to the function caller. For example, the following function returns the square of its argument, x , where x is a number. If the value is omitted, undefined is returned instead.


1 Answers

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

like image 106
georg Avatar answered Sep 18 '22 11:09

georg