javascript 函數不同於其他的語言,每個函數都是作為一個物件被維護和運行的。透過函數物件的性質,可以很方便的將一個函數賦值給一個變數或將函數傳遞為參數。在繼續講述之前,先看一下函數的使用語法:
以下是引用片段:
function func1(…){…}
var func2=function(…){…};
var func3=function func4(…){…};
var func5=new Function();
複製代碼代碼如下:
<script type="text/javascript">
// 1, 方法呼叫模式
// 當一個函數被儲存為物件的屬性時,我們稱之它為該物件的一個方法,那麼this被綁定到該物件上
var myObject={
name : "myObject" ,
value : 0 ,
increment : function(num){
this.value += typeof(num) === 'number' ? num : 0;
return this;
} ,
toString : function(){
return '[Object:' + this.name + ' {value:' + this.value + '}]';
}
}
alert(myObject.increment(10).increment(20).toString()); // [Object:myObject {value:30}]
// 2, 函數呼叫模式
// 當一個函數並非一個物件的函數時,那麼它被當作一個函數來調用,this被綁定到全域物件上。這是語言設計的一個錯誤。倘若語言設計正確,當內部函數呼叫時,this應該仍然綁定到外部函數的this變數上
var myObject={
name : "myObject" ,
value : 0 ,
increment : function(num){
this.value += typeof(num) === 'number' ? num : 0;
return this;
} ,
toString : function(){
return '[Object:' + this.name + ' {value:' + this.value + '}]';
},
getInfo: function(){
var self=this;
return (function(){
//return this.toString(); // 內部匿名函數中this指向了全域物件window,輸出[object Window]
return self.toString(); // 定義一個變數selft並給它賦值為this,那麼內部函數透過該變數存取到指向該物件的this
})();
}
}
alert(myObject.increment(10).increment(20).toString()); // [Object:myObject {value:30}]
// 3, 建構器呼叫模式
// JavaScript是一門基於原型繼承的語言, 這表示物件可以直接從其他物件繼承屬性, 該語言是無類別的。
// 如果一個函數前面帶著new來調用,那麼將會建立一個隱藏連接到該函數的prototype成員的新對象,同時this將會被綁定到建構函數的實例上。
function MyObject(name){
this.name = name || 'MyObject';
this.value=0;
this.increment = function(num){
this.value += typeof(num) === 'number' ? num : 0;
};
this.toString = function(){
return '[Object:' + this.name + ' {value:' + this.value + '}]';
}
this.target = this;
}
MyObject.prototype.getInfo = function(){
return this.toString();
}
// 同時建立一個MyObject.prototype對象,myObject繼承了MyObject.prototype的所有的屬性, this綁定到了MyObject的實例上
var myObject = new MyObject();
myObject.increment(10);
alert(myObject.value); //10
var otherObject = new MyObject();
otherObject.increment(20);
alert(otherObject.value); //20
alert(myObject.target===myObject); // ture
alert(myObject.target.getInfo()); // [Object:MyObject {value:10}]
// 4, Apply 呼叫模式
// JavaScript是一門函數式的物件導向程式語言,所以函數可以擁有方法。 函數的apply方法,如同該物件擁有此方法,此時this指向該物件。
// apply接收兩個參數,第一個是要綁定的物件(this指向的物件),第二個是參數陣列.
function MyObject(name){
this.name = name || 'MyObject';
this.value = 0;
this.increment = function(num){
this.value += typeof(num) === 'number' ? num : 0;
};
this.toString=function(){
return '[Object:'+this.name+' {value:'+this.value+'}]';
}
this.target=this;
}
function getInfo(){
return this.toString();
}
var myObj = new MyObject();
alert(getInfo.apply(myObj)); //[Object:MyObject {value:0}], this指向myObj
alert(getInfo.apply(window)); //[object Window], this指向window
// for and while
function func(a,b){
alert(a); // 1
alert(b); // 2
for(var i=0;i<arguments.length;i++){
alert(arguments[i]); // 1, 2, 1, 2, 3
}
var i=0;
while(i<arguments.length){
alert(arguments[i]); // 1, 2, 1, 2, 3
i=i+1;
}
}
func(1,2,3);
var i=0
for (i=0;i<=10;i++) {
document.write("The number is " + i + "<br/>")
}
</script>