最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

expression - Does JavaScript have the concept of l-value and r-value? - Stack Overflow

programmeradmin2浏览0评论

In JavaScript, if you put some kind of expression on the left side of an assignment expression, the engine will throw a ReferenceError. For example,

// 'this' on the left side
this = window; // ReferenceError: Invalid left-hand side in assignment

or

// function call expression on the left side
var a;
var fn = function() {return a};
a === fn(); // true
a = 1; // 1
fn() = 5; // ReferenceError: Invalid left-hand side in assignment

or

var a;
a = 1; // 1
(a) = 2; // 2
(1, a) = 3; // ReferenceError: Invalid left-hand side in assignment

My questions are:

  1. Does JavaScript also have the concept of l-value and r-value as C?

  2. Why function call expression can not appear on the left-hand of an assignment expression? In the above example, since a === fn(), what's the difference between a = 5 and fn() = 5. I know that ES5 spec mandates this, but why is it designed like that?

In JavaScript, if you put some kind of expression on the left side of an assignment expression, the engine will throw a ReferenceError. For example,

// 'this' on the left side
this = window; // ReferenceError: Invalid left-hand side in assignment

or

// function call expression on the left side
var a;
var fn = function() {return a};
a === fn(); // true
a = 1; // 1
fn() = 5; // ReferenceError: Invalid left-hand side in assignment

or

var a;
a = 1; // 1
(a) = 2; // 2
(1, a) = 3; // ReferenceError: Invalid left-hand side in assignment

My questions are:

  1. Does JavaScript also have the concept of l-value and r-value as C?

  2. Why function call expression can not appear on the left-hand of an assignment expression? In the above example, since a === fn(), what's the difference between a = 5 and fn() = 5. I know that ES5 spec mandates this, but why is it designed like that?

Share Improve this question edited Jun 11, 2015 at 3:05 leslie.zhang asked Jun 11, 2015 at 1:54 leslie.zhangleslie.zhang 3914 silver badges14 bronze badges 5
  • fn() is an expression and not a variable reference, and you can't assign anything to that. – Ja͢ck Commented Jun 11, 2015 at 1:57
  • But the evaluation order is from left to right, if fn() is evaluated first, it will return a, since fn() === a. And a is a variable and can store values. – leslie.zhang Commented Jun 11, 2015 at 2:01
  • 5 it returns the value of a, not a reference to the variable a – Patrick Evans Commented Jun 11, 2015 at 2:04
  • a === fn() doesn't mean they are the same memory location, and if it were, it would have COW semantics ... so, assignment to an expression is meaningless. – Ja͢ck Commented Jun 11, 2015 at 2:28
  • Hi @Patrick Evans, would you elaborate with an answer? – leslie.zhang Commented Jun 11, 2015 at 3:13
Add a ment  | 

3 Answers 3

Reset to default 3

According to the specification of assignments, the following expressions are not valid as the target for an assignment:

this
Literal
ArrayLiteral
ObjectLiteral
FunctionExpression
ClassExpression
GeneratorExpression
RegularExpressionLiteral
TemplateLiteral

For example, the following assignments are invalid too:

this    = "bar"; // this
"foo"   = "bar"; // Literal
/foo/   = "bar"; // RegularExpressionLiteral
["foo"] = "bar"; // ArrayLiteral

The fact that f() can evaluate to a reference, in case you assign an object to a before calling function, doesn't change the fact that f() is an expression (same as for example 1+a). And assignment can only be applied to a variable. MDN

Likewise, in the JavaScript language's world-view, "this is (necessarily ...) sacrosanct."

The purpose, and therefore the present value, of the this variable, is necessarily-protected from arbitrary modification by programs.

Likewise, "you simply cannot say that 'a function,' whatever it is, 'is equal to '5'." (Quite obviously, one of these is a "function," and the other is "an integer constant," and so, "never the twain shall meet.")

The JavaScript interpreter, like every other language-interpreter on this planet, was designed and built using the same language-implementation strategies (and tools ...) employed by every other language that has ever been invented.

发布评论

评论列表(0)

  1. 暂无评论