(轉)深入理解javascript連續賦值運算式

來源:互聯網
上載者:User

標籤:cli   string   passing   topic   網上   art   com   exp   oba   

引入

今天逛園子的時候看到一道javascript面試題,是關於連續賦值的,正好最近讀jQuery源碼經常看到這種連續賦值的運算式,所以高度興趣。

廢話不多說,來看題:

var a = {n: 1}var b = a;a.x = a = {n: 2}console.log(a.x);console.log(b.x)

答案:

console.log(a.x); // undefinedconsole.log(b.x) //{n:2}
View Code

看到這個答案,我真是百思不得解。。。。 於是網上搜了搜,整理如下:

 以下轉自:http://www.iteye.com/topic/785445

 1、引用(Reference)與GetValue & PutValue 

引用A Reference  is a reference to a property of an object. A Reference consists of two components, the base object and the property name.


“引用”是引用某個對象的一個屬性(可能這個對象並沒有這個屬性),一個引用含“根對象”與“屬性名稱”兩個成員。 
後面以“(根對象,屬性名稱)”來表達一個引用 

引用GetValue (V) 
1. If Type(V) is not Reference, return V. 
2. Call GetBase(V). 
3. If Result(2) is null, throw a ReferenceError exception. 
4. Call the [[Get]] method of Result(2), passing GetPropertyName(V) for the property name. 
5. Return Result(4).


GetValue,即取值操作,返回的是確定的值,而不是引用。(可以理解為變數與變數的值,或指標與指標指向的對象) 

引用PutValue (V, W) 
1. If Type(V) is not Reference, throw a ReferenceError exception. 
2. Call GetBase(V). 
3. If Result(2) is null, go to step 6. 
4. Call the [[Put]] method of Result(2), passing GetPropertyName(V) for the property name and W for the value. 
5. Return. 
6. Call the [[Put]] method for the global object, passing GetPropertyName(V) for the property name and W for the 
value. 
7. Return.


PutValue操作只對引用生效,在ECMAScript的描述中,修改對象的屬性都是通過Refrence + PutValue進行的 
(ECMAScript是為了便於表達而引入Reference這個類型,實際上JS語言中並無此類型。The internal Reference type is not a language data type. It is defined by this specification purely for expository 
purposes.) 

2、成員運算式(MemberExpression)解釋過程 

引用The production MemberExpression : MemberExpression [ Expression ] is evaluated as follows: 
1. Evaluate MemberExpression. 
  53 
2. Call GetValue(Result(1)). 
3. Evaluate Expression. 
4. Call GetValue(Result(3)). 
5. Call ToObject(Result(2)). 
6. Call ToString(Result(4)). 
7. Return a value of type Reference whose base object is Result(5) and whose property name is Result(6).


著重看第7步:a value of type Reference 

3、賦值運算式解析 

引用The production AssignmentExpression : LeftHandSideExpression = AssignmentExpression is evaluated as follows: 
1. Evaluate LeftHandSideExpression. 
2. Evaluate AssignmentExpression. 
3. Call GetValue(Result(2)). 
4. Call PutValue(Result(1), Result(3)). 
5. Return Result(3).


這裡可以看到左側得出的是引用,右側調用GetValue取得的是確定值。 

那麼開始分析a.b = a = {n:2}這個運算式,先假設{n:1}這個對象為OBJ1,{n:2}為OBJ2,全域為GLOBAL。 

它的解析如下: 
a.b = Expression1 
Expression1為另一個賦值運算式: 
a = {} 

首先計算a.b = Expression1,按(3)中賦值運算式運行步驟 
step1先得到引用(OBJ1, "b") 
step2解析Expression1{ 
   Expression1解析 
   step1得到引用(GLOBAL, "a") 
   step2得到一個對象OBJ2 
   step3取值,仍是OBJ2 
   step4將引用(GLOBAL, "a")賦值為step3結果 
   step5返回OBJ2 
} 
step3取值,結果同樣為OBJ2 
step4將(OBJ1, "b")賦值為OBJ2 
step5返回OBJ2 

最終結果: 
OBJ1: {n:1, b:OBJ2} 
OBJ2: {n:2} 
a : OBJ2 

PS: 
我們常說賦值運算是從右至左,是指右邊先結合 
所以a.b = a = {n:2}解析為了a.b = ( a = {n:2}),而不會解析為(a.b = a) = {n:2} 
如果理解為右邊先運算就會有誤解了,雖然右邊先賦值成功。 

----------------------------分割線---------------------------

附上ECMA262文檔:ECMA262

 

(轉)深入理解javascript連續賦值運算式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.