只有執行力的叫三級工程師
Javascript
閉包
函數可以訪問它被創建時所處的上下文環境,這叫做閉包。
函數(function)與對象
JS中一切皆對象,function就是對象。
this
1.當一個函數被保存爲對象的一個屬性時,我們稱它爲一個方法。當一個方法被調用時this被綁定到該對象。(這是個比較正常的情況)
2.函數不作爲對象的屬性,也就是當做函數來調用的時候,函數內部的this被綁定到全局變量。
3.(構造器調用模式)
var Que = function(string){
this.status = string;
}
這時跟第一種情況差不多,是比較正常的情況。
apply()和call()兩個方法
栗子:對象1.方法.apply(對象2 , …)
這兩個方法的作用一樣,只是參數的形式不一樣。都是將對象2與對象1替換。
這個方法體中的this
原來都是指向的對象1(也可能指向全局標量windows),apply
以後方法體中的this指向的就編程對象2了。
手寫bind()
Function.prototype.bind = function(that){
var method = this,
slice = Array.prototype.slice;
args = slice.apply(arguments,[1]);
return function(){
return method.apply(that,args.concat(slice.apply(arguments,[0])));
};
};
var x = function(){
return this.value;
}.bind({value:666});
alert(x());// 666
// 書上抄的,還沒有完全了理解...
對象的屬性
Person.name與Person[name]這種寫法等價。
JQuery
擴展/插件
JQuery擴展分兩種:
1.對JQuery本身的擴展
調用如$.foo();
2.對JQuery所選對象擴展方法
調用如$('#myF').foo();
對JQuery本身的擴展
寫法如下:
$.foo = function(){
alert("foo");
}
//調用方法
$.foo();
$.extend({
foo:function(){
alert("foo");
},
bar:function(){
alert("bar");
}
})
//調用方法
$.foo();
$.bar();
$.myPlugin = {
foo:function(){
alert("foo");
},
bar:function(){
alert("bar");
}
}
//調用方法
$.myPlugin.foo();
$.myPlugin.bar();
對JQuery所選對象擴展方法
寫法如下:
$.fn.foo = function(){
alert("hello jquery");
}
$('#myF').foo()
(function($){
$.fn.extend({
pluginName:function(opt,callback){
alert("foo");
}
})
})(jQuery);
$("#myF").pluginName();
(function (jq) {
jq.fn.foo = function (obj) {
alert(obj);
};
})(jQuery)
$("#myF").foo("hello jquery");
……