js中function()使用方法
更新時(shí)間:2013年12月24日 15:29:52 作者:
通過(guò)函數(shù)對(duì)象的性質(zhì),可以很方便的將一個(gè)函數(shù)賦值給一個(gè)變量或者將函數(shù)作為參數(shù)傳遞,下面為大家介紹下函數(shù)的使用語(yǔ)法
javascript 函數(shù)不同于其他的語(yǔ)言,每個(gè)函數(shù)都是作為一個(gè)對(duì)象被維護(hù)和運(yùn)行的。通過(guò)函數(shù)對(duì)象的性質(zhì),可以很方便的將一個(gè)函數(shù)賦值給一個(gè)變量或者將函數(shù)作為參數(shù)傳遞。在繼續(xù)講述之前,先看一下函數(shù)的使用語(yǔ)法:
以下是引用片段:
function func1(…){…}
var func2=function(…){…};
var func3=function func4(…){…};
var func5=new Function();
<script type="text/javascript">
// 1, 方法調(diào)用模式
// 當(dāng)一個(gè)函數(shù)被保存為對(duì)象的一個(gè)屬性時(shí),我們稱之它為該對(duì)象的一個(gè)方法,那么this被綁定到該對(duì)象上
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, 函數(shù)調(diào)用模式
// 當(dāng)一個(gè)函數(shù)并非一個(gè)對(duì)象的函數(shù)時(shí),那么它被當(dāng)作一個(gè)函數(shù)來(lái)調(diào)用,this被綁定到全局對(duì)象上。這是語(yǔ)言設(shè)計(jì)的一個(gè)錯(cuò)誤。倘若語(yǔ)言設(shè)計(jì)正確,當(dāng)內(nèi)部函數(shù)調(diào)用時(shí),this應(yīng)該仍然綁定到外部函數(shù)的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(); // 內(nèi)部匿名函數(shù)中this指向了全局對(duì)象window, 輸出 [object Window]
return self.toString(); // 定義一個(gè)變量selft并給它賦值為this,那么內(nèi)部函數(shù)通過(guò)該變量訪問(wèn)到指向該對(duì)象的this
})();
}
}
alert(myObject.increment(10).increment(20).toString()); // [Object:myObject {value:30}]
// 3, 構(gòu)造器調(diào)用模式
// JavaScript是一門基于原型繼承的語(yǔ)言, 這意味著對(duì)象可以直接從其他對(duì)象繼承屬性, 該語(yǔ)言是無(wú)類別的。
// 如果一個(gè)函數(shù)前面帶上new來(lái)調(diào)用,那么將創(chuàng)建一個(gè)隱藏連接到該函數(shù)的prototype成員的新對(duì)象,同時(shí)this將會(huì)被綁定到構(gòu)造函數(shù)的實(shí)例上。
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();
}
// 同時(shí)創(chuàng)建一個(gè)MyObject.prototype對(duì)象,myObject繼承了MyObject.prototype的所有的屬性, this綁定到了MyObject的實(shí)例上
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 調(diào)用模式
// JavaScript是一門函數(shù)式的面向?qū)ο缶幊陶Z(yǔ)言,所以函數(shù)可以擁有方法。 函數(shù)的apply方法,如同該對(duì)象擁有此方法,此時(shí)this指向該對(duì)象。
// apply接收兩個(gè)參數(shù),第一個(gè)是要綁定的對(duì)象(this指向的對(duì)象),第二個(gè)是參數(shù)數(shù)組.
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>
以下是引用片段:
function func1(…){…}
var func2=function(…){…};
var func3=function func4(…){…};
var func5=new Function();
復(fù)制代碼 代碼如下:
<script type="text/javascript">
// 1, 方法調(diào)用模式
// 當(dāng)一個(gè)函數(shù)被保存為對(duì)象的一個(gè)屬性時(shí),我們稱之它為該對(duì)象的一個(gè)方法,那么this被綁定到該對(duì)象上
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, 函數(shù)調(diào)用模式
// 當(dāng)一個(gè)函數(shù)并非一個(gè)對(duì)象的函數(shù)時(shí),那么它被當(dāng)作一個(gè)函數(shù)來(lái)調(diào)用,this被綁定到全局對(duì)象上。這是語(yǔ)言設(shè)計(jì)的一個(gè)錯(cuò)誤。倘若語(yǔ)言設(shè)計(jì)正確,當(dāng)內(nèi)部函數(shù)調(diào)用時(shí),this應(yīng)該仍然綁定到外部函數(shù)的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(); // 內(nèi)部匿名函數(shù)中this指向了全局對(duì)象window, 輸出 [object Window]
return self.toString(); // 定義一個(gè)變量selft并給它賦值為this,那么內(nèi)部函數(shù)通過(guò)該變量訪問(wèn)到指向該對(duì)象的this
})();
}
}
alert(myObject.increment(10).increment(20).toString()); // [Object:myObject {value:30}]
// 3, 構(gòu)造器調(diào)用模式
// JavaScript是一門基于原型繼承的語(yǔ)言, 這意味著對(duì)象可以直接從其他對(duì)象繼承屬性, 該語(yǔ)言是無(wú)類別的。
// 如果一個(gè)函數(shù)前面帶上new來(lái)調(diào)用,那么將創(chuàng)建一個(gè)隱藏連接到該函數(shù)的prototype成員的新對(duì)象,同時(shí)this將會(huì)被綁定到構(gòu)造函數(shù)的實(shí)例上。
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();
}
// 同時(shí)創(chuàng)建一個(gè)MyObject.prototype對(duì)象,myObject繼承了MyObject.prototype的所有的屬性, this綁定到了MyObject的實(shí)例上
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 調(diào)用模式
// JavaScript是一門函數(shù)式的面向?qū)ο缶幊陶Z(yǔ)言,所以函數(shù)可以擁有方法。 函數(shù)的apply方法,如同該對(duì)象擁有此方法,此時(shí)this指向該對(duì)象。
// apply接收兩個(gè)參數(shù),第一個(gè)是要綁定的對(duì)象(this指向的對(duì)象),第二個(gè)是參數(shù)數(shù)組.
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>
相關(guān)文章
jQuery實(shí)現(xiàn)文字自動(dòng)橫移
本文詳細(xì)介紹了通過(guò)jquery尺寸相關(guān)函數(shù)實(shí)現(xiàn)文字自動(dòng)橫移的方法。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01淺談layer的iframe彈窗給里面的標(biāo)簽賦值的問(wèn)題
下面小編就為大家?guī)?lái)一篇淺談layer的iframe彈窗給里面的標(biāo)簽賦值的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11對(duì)javascript的一點(diǎn)點(diǎn)認(rèn)識(shí)總結(jié)《javascript高級(jí)程序設(shè)計(jì)》讀書(shū)筆記
Javascript專為與網(wǎng)頁(yè)交互而設(shè)計(jì)的腳本語(yǔ)言,由下列三個(gè)部門構(gòu)造2011-11-11IE下window.onresize 多次調(diào)用與死循環(huán)bug處理方法介紹
IE下window.onresize多次調(diào)用與死循環(huán)bug處理方法介紹。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-11-11微信端調(diào)取相冊(cè)和攝像頭功能,實(shí)現(xiàn)圖片上傳,并上傳到服務(wù)器
這篇文章主要介紹了微信端調(diào)取相冊(cè)和攝像頭功能圖片上傳服務(wù)器,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05javascript設(shè)計(jì)模式之中介者模式學(xué)習(xí)筆記
這篇文章主要為大家詳細(xì)介紹了javascript設(shè)計(jì)模式之中介者模式學(xué)習(xí)筆記,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02