基本上Java Script的operators和C/C++/JAVA大致相同,所以這邊只有提出一些不同於C/C++的operators。
=== (identical)
如果資料型態相同,內容相同回傳true,否則回傳false。對於primitive type只要內容相同即可。對於reference type(object/array/function)則是要refence到相同的物件。
var n1 = 123;
var n2 = 123;
var no = new Number(123);
alert("n1 == no ? " + (n1 == no));
alert("n1 === no ? " + (n1 === no));
alert("n1 == n2 ? " + (n1 == n2));
alert("n1 === n2 ? " + (n1 === n2));
var s = "123";
var so = new String("123");
alert("s == so ? " + (s == so));
alert("s === so ? " + (s === so));
in operator
當in為 left-side operator時,可以取值。當in為 right-side operator時,則判斷是否存在。
var point = { x:1, y: 1};
var has_x_coord = "x" in point;
alert("x is set ? " + has_x_coord);
alert("x is set ? " + ("z" in point));
for (p in point) {
alert("point." + p + "=" + eval("point." + p));
}
instanceof operator
為判斷是否為該object的instance。
var d = new Date(); alert((d instanceof Date)); alert((d instanceof Object)); alert((d instanceof Number));
>> shift right with sign, >>> Shift right with zero fill
這邊就要提到java script的number是如何儲存的,所有的number在java script中,都是以64-bit的IEEE 754表示。所以,思考一下執行完的結果吧。
alert(-1 >> 1); alert(1 >> 1); alert(-1 >>> 1); alert(1 >>> 1);
typeof return a string indicating the datatype of the operand.
var s = "123";
var d = new Date();
alert("typeof s = " + (typeof s));
alert("typeof d = " + (typeof d));

沒有留言:
張貼留言