var x = .3 - .2; var y = .2 - .1; x == y; // false
var o = {x:1},p = {x:1}; o == p; // => false o === p; // => false var a = [], b = []; a == b; // => false a === b; // => false
值 | 轉換爲 | - | - | - |
---|---|---|---|---|
- | 字符串 | 數字 | 布爾值 | 對象 |
undefined | "undefined" | NaN | false | throws TypeError |
null | "null" | 0 | false | throws TypeError |
true | "true" | 1 | - | new Boolean(true) |
false | "false" | 0 | - | new Boolean(false) |
"" | - | 0 | false | new String("") |
"1.2" | - | 1.2 | true | new String("1.2") |
"sevens" | - | NanN | true | new String("sevens") |
0 | "0" | - | false | new Number(0) |
-0 | "0" | - | false | new Number(-0) |
NaN | "NaN" | - | false | new Number(NaN) |
1 | "1" | - | true | new Number(1) |
{} | 參考3.8.3節 | 參考3.8.3節 | true | |
[] | "" | 0 | true | |
[9] | "9" | 9 | true | |
['a'] | 使用join方法 | NaN | true | |
function(){} | 參考3.8.3節 | NaN | true |
null == undefined; // true "0" == 0; //比較前字符串轉換成數字 true 0 == false; //布爾轉換成數字 true "0" == false; //字符串和布爾值都轉換成數字 true - 對日期對象進行轉換 ````javascript var now = new Date(); typeof (now + 1); // => string , "+"將日期轉換成了字符串 typeof (now - 1); // => Number: "-" 將對象轉換成了數字
var scope = "global"; function f(){ console.log(scope); // undefined var scope = "local"; console.log(scope); // local }
因爲局部變量在整個函數體始終都有定義的,就是說,在函數體內局部變量遮蓋了同名全局變量。可是!只有在執行到var的時候纔會被賦值。所以,上面的代碼至關於:javascript
var scope = "global"; function f(){ var scope; console.log(scope); // undefined scope = "local"; console.log(scope); // local }
var o = {x:1,y:2,z:3}; var a = [],i = 0; for(a[i++] in o) ;
function inherit(p) { if (p == null) throw TypeError(); //若是Object.create()存在,則直接使用它 if (Object.create) return Object.create(p) //不然進一步檢測 var t = typeof p; if (t !== "object" && t !== 'function') throw TypeError(); //定義一個空構造函數 function f() {}; //將其原型屬性設置爲p f.prototype = p; return new f(); }
Object.defineProperty(Object.prototype,"extend", { writable: true, enumerable: false, configurable: true, value: function (o){ //獲得全部自有屬性,包括不可枚舉屬性 var names = Object.getOwnPropertyNames(o); //遍歷 for (var i = 0; i < names.length; i++){ if (names[i] in this) continue; var desc = Object.getOwnPropertyDescriptor(o,names[i]); Object.defineProperty(this,names[i],desc); } } } );
To be Continue.. java