number是以floating point的方式表示,不管是integer或floating point
truthy的功用等同於true, falsy的功用等同於false
null可以被視為是一種特別的object,如果使用typeof(null),則回傳"object"( means "no object")
undefined是一個predefined global variable,如果使用typeof(undefined),則回傳"undefined"
null and undefined both indicate an absence of value and can often be used interchangeably.
The equality operator == considers them to be equal. (Use the strict equality operator === to distinguish them.)
global object是java script中一種比較特別的object,當java script interpreter起動時,它會init一些property,包含property (undefined, Infinity, NaN),function (isNaN(), parseInt()),constructor function (Date(), String()),global object (Math)
我們可以用var global = this; 的方式來參考global object
wrapper object概念上有點類似java的boxing
var s = "test";
s.len = 4;
var t = s.len; // undefined
t並不會等於4,因為s.len = 4這行statement, 會crete一個temp object,並且把"len" property設為4,之後就會delete掉這個object,所以之後再參考這個property時(這個property並不存在),會是undefined
primitive value是immutable,object是mutable
var s = "hello";
s.toUpperCase(); // return "HELLO",但並不會修改到s原本的內容
s; // return "hello"
var o = {x:1}, p = {x:1};
o === p; // false
var a= [], b=[];
a ===b; // false
var a = [];
var b = a;
b[0] = 1;
a[0];
a === b; // true
我們可以直接invoke一些func來達到explicit type conversion
Number("3"); // 3
String(false); // "false"
Boolean([]) // true
Object(3)
某些java script的operator會有implicit type conversion的行為
x + ""; // String(x)
+x; // Number(x)
!!x; // Boolean(x)
object to string conversion
1.如果object有toString(),java script會call此func,如果return回來的值為primitive,js會將它轉為string,再return回去
2.如果object沒有toString(),或是此func並沒有return primitive value,則js轉而呼叫valueOf(),得到primitive value,並將之轉換為string再return回去
3.如果上述2個方法皆無法進行的話,則return TypeError
object to number conversion
1.如果object有valueOf(),js會先呼叫它,將回傳的primitive value轉為number,並return
2.如果object有toString(),則js轉而呼叫它,並將回傳的primitive value轉為number,並return
3.上述皆否的話,則return TypeError
關於variable scope的問題,java script使用function scope,而不採用一般c所使用的block scope
var scope = "global";
function f() {
console.log(scope); // undefined
var scope = "local";
console.log(scope);
}
上述的statement可視為
function f() {
var scope;
console.log(scope);
scope = "local";
console.log(scope);
}
在globla object中,何謂nonconfigurable & configurable
var truevar = 1;
fakevar = 2;
this.fakevar2 = 3;
delete truevar // nonconfigurable
delete fakevar // configurable
delete this.fakevar2 // configurable
留言列表