国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

javascript - Hat das ?ndern von Instanzattributen auch Auswirkungen auf die Inhaltsausgabe an die Konsole vor der ?nderung?
過去多啦不再A夢
過去多啦不再A夢 2017-05-19 10:35:27
0
3
627

Es ist kein Problem mit der Dynamik des Prototyps, es ist ein Problem mit der Konsole
Fügen Sie zuerst meinen Code ein

function Father(){
    this.colors = ["red", "green", "blue"],
    this.sayColor = function(){
    console.log(this.colors);
    };
}
function Child(){}
Child.prototype = new Father();

var child1 = new Child();
child1.sayColor(); // ["red", "green", "blue"] 原始值

child1.colors.push("black"); // 屬性修改
var child2 = new Child();
child2.sayColor(); // ["red", "green", "blue", "black"]
child1.sayColor(); // ["red", "green", "blue", "black"]

Die Anmerkung ist das Ergebnis des normalen Betriebs, aber wenn sie in einem Browser (Firefox und Chrome) ge?ffnet wird, gibt die Konsole drei identische Arrays zurück:

und

Nachdem Sie zum Aktualisieren der Seite geklickt haben, werden normale Ergebnisse zurückgegeben.
Oder console.log改為alert Beim ?ffnen der Seite werden normale Ergebnisse zurückgegeben normal;
Glauben Sie, dass die Art und Weise, wie die Konsole die Ergebnisse ausgibt, anders ist als ich denke? Bitten Sie um Antworten.

過去多啦不再A夢
過去多啦不再A夢

Antworte allen(3)
洪濤

我也遇到過這樣的問題,以下是我提出的問題:
/q/10...

如果你不想看,總的來說console.log 是有惰性求值的問題!

先說結(jié)論:console.log 是不靠譜的由于它并非標(biāo)準(zhǔn)里確定的 API,所以瀏覽器的實(shí)現(xiàn)是沒有標(biāo)準(zhǔn)可言的。有時(shí)候會(huì)出現(xiàn)同步的 console.log 與異步的 console.log 的困惑,也有立刻求值的 console.log 和惰性求值的 console.log 的差異。你遇到的是后者。

補(bǔ)充參考:http://stackoverflow.com/ques...

Peter_Zhu

好吧,問題中已經(jīng)說明不是原型問題,是控制臺(tái)問題。 敲了這么多字,覺得還有點(diǎn)價(jià)值,沒舍得刪...
以下解釋與提問者所想知道的不一致。
我回答的是: 為什么實(shí)例對象的屬性變更會(huì)影響另外一個(gè)實(shí)例?
權(quán)當(dāng)給對原型繼承理解不清的人一個(gè)解釋。

我的Chrome(Mac平臺(tái)下,版本57.0.2987)并沒有出現(xiàn)你說的問題,輸出結(jié)果和期望一致:

  • 第一個(gè)輸出: ["red", "green", "blue"]

  • 第二個(gè)輸出: ["red", "green", "blue", "black"]

  • 第三個(gè)輸出: ["red", "green", "blue", "black"]

解答問題前,請看一個(gè)示例, 可以解釋你遇到的問題。

var father_colors = ["red", "green", "blue"];
var child1_colors = father_colors

console.log(child1_colors);  // ["red", "green", "blue"] 原始值

child1_colors.push("black");
var child2_colors = father_colors;

console.log(child2_colors);  // ["red", "green", "blue", "black"]
console.log(child2_colors);  // ["red", "green", "blue", "black"]

原因

好,現(xiàn)在回到你的問題: 為什么改了child1實(shí)例的屬性,確影響到了child2?

  1. 創(chuàng)建實(shí)例后,實(shí)例的 __proto__ 屬性會(huì)指向父類的 .prototype 屬性, 記住是一個(gè)指向(引用)而不是復(fù)制!

  2. 訪問實(shí)例屬性時(shí),先在實(shí)例自己身上找,如果沒有找到,通過 __proto__屬性去父類的 prototype屬性中尋找: child1.colors 指向 Father.prototype.colors, 因此對colors的操作會(huì)直接影響父類中的引用對象。

  3. child2.colors 也會(huì)去找 Father.prototype.colors, 結(jié)果自然是child1修該colors之后的結(jié)果。

怎樣避免?

child1.colors你重新賦值而不是直接操作,就不會(huì)有問題。 (記住,對父類中的引用類型屬性都不要直接操作?。?/p>

child1.colors = ["red", "green", "blue", "black"];
// 或者用
child1.colors = child1.colors.concat().push["black"];  //concat()方法復(fù)制了一份父類的colors數(shù)組。
滿天的星座

自答一個(gè):
正如采納答案所說,是console.log的惰性求值問題;
當(dāng)輸出的內(nèi)容為引用類型中的ArrayObject時(shí),很有可能會(huì)出現(xiàn)問題中的情況;

目前我看到的最佳解決方法是:
將輸出的內(nèi)容改為console.log(JSON.stringify(yourArray));
不會(huì)改變輸出的類型和內(nèi)容,卻規(guī)避了console.log的惰性求值問題;

最后感謝所有回答者!

Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage