Math.PI相関式演算

2805 ワード

あまり新しくなく、ネットワークに伝わるコード:
 
var a = (++Math.PI); 
alert(a); //1
alert(Math.PI); //2

var b = (Math.PI++); 
alert(b); //3
alert(Math.PI); //4

var c = Math.PI = (++Math.PI); 
alert(c); //5
alert(Math.PI); //6


var d = Math.PI = (Math.PI++); 
alert(d); //7
alert(Math.PI); //8


var e = Math.PI = (Math.PI + 1);
alert(e); //9
alert(Math.PI); //10

 
 
Math.PIは他の言語から見ればシステムの定数であり、それを修正すればコンパイルは通じないが、javascriptは強いフォールトトレランス(包容性?)を持っているので、彼はあなたに注意しないで、これを理解するには規範を見ないとだめだ.
 
ぶんせき
 
ECMAScript 262 :
 
PI
 
15.8.1.6 PI The number value for π, the ratio of the circumference of a circle to its diameter, which is approximately 3.1415926535897932. This property has the attributes { DontEnum, DontDelete, ReadOnly }.
システム属性のみreadonlyがあり、私たちが定義した一般属性は設定できませんが、ReadOnlyは定数に相当するのではないでしょうか.
ReadOnly
8.6.1 ReadOnly The property is a read-only property. Attempts by ECMAScript code to write to the property will be ignored. (Note, however, that in some cases the value of a property with the ReadOnly attribute may change over time because of actions taken by the host environment; therefore “ReadOnly” does not mean “constant and unchanging”!)
一般的な状況を見ると、readonly属性は変更できませんが、間違っていません.つまり、値を付けて修正しても無駄です.定数という意味でもない!
では、2,4,6,8,10個のコードalertは3.14であることがわかりました.
また3 alertも説明する必要はありませんが、増加前の結果を返すのは3.14です.
では1 alertは3.14で、PI readonlyは変えられないからですか?エラー
 
 
じこぞうふくえんざんし
 
 
接頭辞の自己増加演算子の戻り値に関する問題:
11.4.4 Prefix Increment Operator The production UnaryExpression :
++ UnaryExpression is evaluated as follows: 1.    Evaluate UnaryExpression.
2.    Call    GetV alue(Result(1)).
3.    Call ToNumber(Result(2)).
4.    Add the value 1 to Result(3), using the same rules as for the + operator (see 11.6.3).
5.    Call    PutV alue(Result(1),    Result(4)). 6.    Return Result(4).
これはResult(4)Mathである.手順5以降のResultではなくPI+1
1 alertがMathであることがわかる.PI+1 = 4.14
 
 
割り当てオペレータ
 
c,d,eは類似している=オペレータが右結合している場合、初期化時に変数値が付与式(Math.PI=++Math.PI)であることを示し、javascriptは各式に1つの値を返します.これは、前文の式の戻り値がいくらであるかに関連します.
11.13.1    Simple Assignment ( = )
The production AssignmentExpression : LeftHandSideExpression = AssignmentExpression is evaluated as follows: 1.    Evaluate LeftHandSideExpression.
2.    Evaluate AssignmentExpression.
3.    Call    GetV alue(Result(2)).
4.    Call    PutV alue(Result(1),    Result(3)). 5.    Return Result(3).
前述の自増仕様では++Mathを得ることができる.PIが返す4.14はResult 2,Result 3であり、同様に、4ステップ目の付与後、左端の値Mathではなく、付与式の右端の値(++Math.PI)を返す.PIは、(Math.PI=++Math.PI)が++Mathを返す.PI==4.14、すなわち、連続付与演算は、付与中の変数が変化したかどうかにかかわらず、右端の値のみを返す.