1樓:
如果return的值是object,那麼就返回這個return的object,前面的this就都是無效的;
如果return的值型別不是object,就還是返回this;
2樓:
搬運一下Annotated ES5
11.2.2 The new Operator #
The production NewExpression :new NewExpression is evaluated as follows:
Let ref be the result of evaluating NewExpression.
Let constructor be GetValue(ref).
If Type(constructor) is not Object, throw a TypeError exception.
If constructor does not implement the [[Construct]] internal method, throw a TypeError exception.
Return the result of calling the[[Construct]] internal method on constructor, providing no arguments (that is, an empty list of arguments).
下面是[[Constructor]]的定義:
13.2.2 [[Construct]]
When the [[Construct]] internal method for a Function object F is called with a possibly empty list of arguments, the following steps are taken:
Let obj be a newly created native ECMAScript object.
Set all the internal methods of obj as specified in 8.12.
Set the [[Class]] internal property of obj to "Object".
Set the [[Extensible]] internal property of obj to true.
Let proto be the value of calling the [[Get]] internal property of F with argument "prototype".
If Type(proto) is Object, set the [[Prototype]] internal property of obj to proto.
If Type(proto) is not Object, set the [[Prototype]] internal property of obj to the standard built-in Object prototype object as described in 15.2.4.
Let result be the result of calling the [[Call]] internal property of F, providing obj as the this value and providing the argument list passed into [[Construct]] as args.
If Type(result) is Object then return result.
Return obj.
最關鍵的就是9,10。
返回同乙個物件可以用來實現單例模式。
3樓:貘吃饃香
瀉藥這事兒其實就是規範規定
不過每版規範的說道都有變化而已
按ES5來說
規矩是Type(result) is Object then return result.說明如果new過程中function返回了物件
那就把這玩意直接扔出來
否則就是
Return obj
把構造好的綁上prototype啊、this啊啥玩意的
obj 扔出來
到ES6呢
規矩說道又變了點
If result.[[type]] is return, then ……
它多了個[[type]]
其實呢是讓每個操作完成時候有個記錄物件
(The Completion Record Specification Type)
記錄結果而已
比如這裡的操作步驟
result 就是個結果記錄集
裡頭有[[type]]啊[[value]]啊[[target]]啊
[[type]]的值表示了操作狀態
有 return 啊、throw 啊等等吧
(可以去查規矩裡的Table8)
就是說如果是正常返回了
(不是 throw了什麼的)
就去查If Type(result.[[value]]) is Object, return NormalCompletion(result.[[value]]).
看看這貨返回值是不是個Object
如果是呢
就呼叫NormalCompletion處理
普通結果啦
不就是把
result再包成個The Completion Record Specification Type
往外扔唄
只不過此時
[[type]] 值就不能是 return了
明擺著得變 normal 不是
[[value]] 不就是 return [[value]]麼
也就是內個Object了
這一折騰就把返回值包成正常結果記錄扔出來
如果 result.[[value]]) 不是 Object呢
就往下走
Else, ReturnIfAbrupt(result).
Return envRec.GetThisBinding().把this繫結結果返回
也就是正常建構函式建立繫結流程
所以我扯那麼多
這全是沒用廢話……
沒準過幾年規矩的說頭還得變
其實你完全記住了這是規矩就行
遇上返回Object就返回自身
否則返回構造的Object
C 的函式返回值是如何構造的?
你如果是想問為什麼少輸出乙個new的話,那是因為F在隱式建立返回值物件的過程中,呼叫的是複製建構函式A const A 而不是預設的無參建構函式A 所以你自定義的無參建構函式A 並沒有捕獲到這次構造。include using namespace std struct A A A const A o...
Java 中,構造方法是否有返回值?
黃哥 正確的使用搜尋引擎是多麼重要!搜尋結果中,stackoverflow 網上的問答,就能找到你要的答案。建議用 搜狗英文 搜尋。 invalid s 沒有。物件的建立分為兩步。第一步是從某個位置獲得一塊記憶體,第二步是把這塊記憶體 初始化 而乙個物件如何初始化,只有它的設計者知道。建構函式就是這...
為什麼不把要返回元素直接當成函式的返回值返回呢?
phoenix bool get elem elem get throw elem get bool 回字的四種寫法 iter get tuple get 想到再加 ant 有些時候乙個返回不夠用啊,函式中好幾個值需要傳出時只能通過引數傳遞實現,而不能簡單的通過函式返回值。當然,你也可以設計乙個資料...