前面一句話: 挖掘知識的邊界,摳的越細,深度和廣度才越高,才越牛.javascript
(1) == ===的區別??java
null == undefined //true
'' == false //true
總結 ==不靠譜, 只用===數組
(2) 不使用continue;閉包
效率低,出處:javascript語言精粹111頁app
(2) var a = {};dom
a.b='xx';
a['delete'] = 'yy'; ///關鍵詞也無所謂了函數
(3) delete 刪屬性
全局變量和局部變量刪除不掉???性能
var aaaa = 123;
delete aaaa;
console.log(aaaa); //123優化
window.aaaa = 123;
delete window.aaaa;
console.log(window.aaaa); //undefinedthis
function a(){
var a= 123;
delete a;
console.log(a); //123
}a();
設爲a=null;祈禱上天,等待垃圾回收機制回收.
注:當使用var聲明一個變量時,建立的這個屬性是不可配置的,也就是說沒法經過delete運算符刪除
var name=1 ->不可刪除
sex=」girl「 ->可刪除
this.age=22 ->可刪除
(4) typeof instanceof
typeof 2 === 'number' //true
typeof new Number(2) === 'number' //false
typeof '2' === 'string' //true
typeof new String('2') === 'string' //false
typeof true === 'boolean' //true
typeof new Boolean(true) === 'boolean' //false
typeof [] === 'object' //true
var a = function(){}; typeof a === 'function'; //true
[] instanceof Object //true
2 instanceof Number //false
new Number(2) instanceof Number //true
var a = function(){}
var b = function(){};
a.prototype = new b();
new a() instanceof b; //true
// typeof 能判斷 基本類型 和 function
// instanceof 能判斷 自定義對象
//擴展 Object.prototype.toString.call([]) === '[object Array]'; 這樣是最棒的判斷
(4) if語句
if( xxxx ) ? //6種狀況爲false
// null undefined NaN 0 '' false
(4) for in
//遍歷對象
//遍歷鬆散的數組
var a = new Array(10); a[2]=10; a[5]=10; for(var i in a){ console.log(i); }
性能,確實優化了
var a = new Array(10000000);
a[2]=10;
a[5]=10;
var time1 = new Date().getTime();
for(var i in a ){
Math.random()* Math.random()* Math.random() * Math.random();
}
var time2 = new Date().getTime();
for(var i=0; i<a.length; a++){
Math.random()* Math.random()* Math.random() * Math.random();
}
var time3 = new Date().getTime();
console.log(time2-time1); //~100ms
console.log(time3-time2); //~200ms
(5) 函數字面量 與 函數聲明
var a = 2;
function a(){}
console.log(typeof a); //function ? number ?
總結:
變量的聲明被提早到做用域頂部,賦值保留在原地;
聲明式函數整個「被提早」;
函數表達式只有變量「被提早」了,函數沒有「被提早」;
函數的聲明比變量的聲明具備高的優先級。
賦值會根據先後順序進行覆蓋!
var a = function(){
console.log('aaa');
}
function a(){
console.log('bbb');
}
//var b = function b(){} === function b(){}
a();
(6) 函數返回值
var a = function(b,c){
console.log(b+','+c);
}
console.log( a() );
(7) this指向
function a(){
var that = self = this;
this.v = 'aaa';
function b(){
console.log(this);
console.log(this.v);
}
b();
}
new a();
(8) arguments
function a(a,b,c,d){
//var args = [].slice.call(arguments);
}
(9) apply call
function a(){
console.log(this.x+this.y);
}
a.call({x:100,y:100});
function C(){
this.m = function(){
console.log(this.x+this.y);
}
}
var c = new C();
c.m.call({x:100,y:100});
//這個就是this的重定向,原諒我吧,無法解釋的很準確和淺顯易懂,只能多練習
(10) try catch throw 關鍵邏輯加上就好
var a = 'aaa';
try{
a = xxxx(); //不存在的方法
}catch(e){
throw(new Error('error'));
a = 0;
}
console.log("go go go"); //這裏走嗎???
(11) 閉包
for(var i=0; i<2; i++){
}
console.log(i);
function a(){
//只有方法體的纔有做用域
}
var a;
function c(){
var v = 'vvv';
function b(){
return {v:v};
};
a = b();
}
c();
console.log(a.v); //方法體是做用域, 這個你們都知道了, 內部的方法體須要給個口子出來,讓外面能夠訪問到.
(12) 回調
function a(callback){
callback();
}
a(function(){
console.log('aaa');
});
(13) 自執行函數, 也叫自調函數
(function(a){
console.log(a);
})('aaa');
(14) 正則
var regexp = / /img
//i 大小寫
//m 換行
//g 檢索到第一個匹配不中止,檢索所有
(15) prototype
//prototype是什麼?
function a(){
this.v='vvv';
}
console.log(a.prototype); //當前對象,函數也是對象,
console.log(a.prototype.constructor); //當前方法
console.log(a.prototype.constructor.__proto__ === a.__proto__); //true
//prototype是函數的內置屬性,__proto__是對象的內置屬性,都是查找原型鏈的
//prototype使用場景?
var b = {};
//function b(){};
//var b = function(){}
b.prototype.bbb='bbb';
console.log(b.bbb);
//console.log(new b().bbb); //構造器
//prototype咋玩? (下面的都是很差的用法)
function a(){};
a.prototype = function(){ console.log("aaa") };
console.log( new a.prototype() );
// function a(){}
// function b(){}
// a.prototype = {aaa:'aaa'};
// b.prototype = a.prototype;
// var aa1 = new a(),
// aa2 = new a(),
// bb1 = new b();
// aa1.prototype.aaa='xxx';
// console.log( aa2.aaa );
// console.log( bb1.aaa );
// function a(){}
// function b(){ this.bbb='bbb'};
// a.prototype = new b();
// console.log(new a().bbb );
//-----4 實際上是這樣的
Object.create();