jquery的arguments物件使用
阿新 • • 發佈:2018-12-03
jquery的arguments物件使用
.函式引數Arguments物件
每建立一個函式,該函式就會隱式建立一個arguments物件,他包含有實際傳入引數的資訊。
1.length 檢測實際傳入引數的個數
2.callee 對本身的呼叫
訪問傳入引數的具體的值。([下標])
function fun (a,b) { for (var i=0; i<arguments.length; i++) { alert(arguments[i]) } }
arguments為js內建的物件。
function myArray () {
var lengs= arguments.length;
for (var i=0; i<lengs; i++) {
this[i]=arguments[i];
}
}
var arr=new myArray(1,2,3);
alert(arr[0]);
arguments是指向實參物件的引用,實參物件是一個類陣列物件 .
arguments 的索引 從 0, 1,2,....遞增,與實參逐個對應.
arguments.length 屬性代表實參的個數
arguments一定不是陣列, 是長的比較像陣列的一個物件,雖然也有length屬性
arguments每個函式都會有,因此,arguemnts只會在內部找自身的arguments,無法引用到外層的arguments
// 求圓形面積,矩形面積, 三角形面積 function area () { if(arguments.length == 1) { alert(3.14 * arguments[0] * arguments[0]); } else if(arguments.length == 2) { alert(arguments[0] * arguments[1]); } else if(arguments.length == 3) { alert(arguments[0] + arguments[1] + arguments[2]); } else { return null; } } //呼叫 area(10,20,30);
具體實現
/* Simple JavaScript Inheritance
* By John Resig http://ejohn.org/
* MIT Licensed.
*/
// Inspired by base2 and Prototype
(function(){
//initializing是為了解決我們之前說的繼承導致原型有多餘引數的問題。當我們直接將父類的例項賦值給子類原型時。是會呼叫一次父類的建構函式的。所以這邊會把真正的構造流程放到init函式裡面,通過initializing來表示當前是不是處於構造原型階段,為true的話就不會呼叫init。
//fnTest用來匹配程式碼裡面有沒有使用super關鍵字。對於一些瀏覽器`function(){xyz;}`會生成個字串,並且會把裡面的程式碼弄出來,有的瀏覽器就不會。`/xyz/.test(function(){xyz;})`為true代表瀏覽器支援看到函式的內部程式碼,所以用`/\b_super\b/`來匹配。如果不行,就不管三七二十一。所有的函式都算有super關鍵字,於是就是個必定匹配的正則。
var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
// The base Class implementation (does nothing)
// 超級父類
this.Class = function(){};
// Create a new Class that inherits from this class
// 生成一個類,這個類會具有extend方法用於繼續繼承下去
Class.extend = function(prop) {
//保留當前類,一般是父類的原型
//this指向父類。初次時指向Class超級父類
var _super = this.prototype;
// Instantiate a base class (but only create the instance,
// don't run the init constructor)
//開關 用來使原型賦值時不呼叫真正的構成流程
initializing = true;
var prototype = new this();
initializing = false;
// Copy the properties over onto the new prototype
for (var name in prop) {
// Check if we're overwriting an existing function
//這邊其實就是很簡單的將prop的屬性混入到子類的原型上。如果是函式我們就要做一些特殊處理
prototype[name] = typeof prop[name] == "function" &&
typeof _super[name] == "function" && fnTest.test(prop[name]) ?
(function(name, fn){
//通過閉包,返回一個新的操作函式.在外面包一層,這樣我們可以做些額外的處理
return function() {
var tmp = this._super;
// Add a new ._super() method that is the same method
// but on the super-class
// 呼叫一個函式時,會給this注入一個_super方法用來呼叫父類的同名方法
this._super = _super[name];
// The method only need to be bound temporarily, so we
// remove it when we're done executing
//因為上面的賦值,是的這邊的fn裡面可以通過_super呼叫到父類同名方法
var ret = fn.apply(this, arguments);
//離開時 儲存現場環境,恢復值。
this._super = tmp;
return ret;
};
})(name, prop[name]) :
prop[name];
}
// 這邊是返回的類,其實就是我們返回的子類
function Class() {
// All construction is actually done in the init method
if ( !initializing && this.init )
this.init.apply(this, arguments);
}
// 賦值原型鏈,完成繼承
Class.prototype = prototype;
// 改變constructor引用
Class.prototype.constructor = Class;
// 為子類也新增extend方法
Class.extend = arguments.callee;
return Class;
};
})();