js函數的各種寫法與調用

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   io   2014   ar   

以下是我見過的各種js函數的各種寫法以及調用,雖然有些寫法及其調用我不清楚其專業術語叫啥,但並不影響我寫一個總結筆記。

 

我們剛開始接觸js語音,經常看到的這種名叫“使用function關鍵字來定義函數”的寫法,如:

function f(e){
alert(e);
}
f("hello world");

把函數賦值給一個變數,這種大概叫“運算式方式定義函數”吧,如:

var f=function(e){
alert(e);
}
f("hello world");

如上,如果省略了函數名,這種也叫“匿名函數”,當然也可以不匿名,給一個函數名,這在用於遞迴函式時就用到了,如:

var fact=function factTemp(n){
if(n<=1) return 1;
else return n*factTemp(n-1);
}
fact(3); // return 6

還有一種叫“使用建構函式”來定義函數,如:

var f=new Function("x","y","alert(x*y);");
f(2,3); //==>6

以上是三種定義函數的寫法及其調用,但是調用還有其他方式:

用call()函數間接調用,如:

function person(name){    this.name=name;    this.sayHi=function(age,blogs){        alert("My name is: "+this.name+";\n age:"+age+";\n blogs:"+blogs);        }}function me(name){    this.name=name;    }var person1=new person("function person");var me1=new me("function me");person1.sayHi.call(me1,24,"http://www.cnblogs.com/xiaomou2014");  //本來me1是沒有sayHi函數的,經過用call就調用了personal裡的sayHi函數了。

同樣類似於call的同類函數apply也同樣能實現這樣子的功能,只不過他的第二個參數是數組而已,如:

function person(name){    this.name=name;    this.sayHi=function(age,blogs){        alert("My name is: "+this.name+";\n age:"+age+";\n blogs:"+blogs);        }}function me(name){    this.name=name;    }var person1=new person("function person");var me1=new me("function me");person1.sayHi.apply(me1,[24,"http://www.cnblogs.com/xiaomou2014"]);  

 

函數可以賦值給變數,同時也可以把他直接賦值給對象的屬性,如:

var o={ square:function(x){ return x*x; } };

var y=o.square(2);  //y=4;

 

有時候我們定於了一個函數後需要他馬上運行,這貌似也挺多見於js外掛程式,如:

(function(){
alert("hello world");
})();

也可以給他一個函數名:

(function f(){
alert("hello world");
})();

第二種:

(function(){
alert("hello world");
}());

同樣的我們也可以給他一個函數名:

(function f(){
alert("hello world");
}());

當然也可以給它一個參數:

(function(e){
alert(e);
}("hello world"));

看別人的外掛程式,你會發現人家開頭處加了一個";",這樣就算頁面js有錯誤,載入運行他的外掛程式也能保證運行,如:

;(function(e){
alert(e);
}("hello world"));

 

如果一個函數的參數很多,那麼我們調用函數的時候並不能很好的記住他的順序,把參數封裝成對象,然後把對象裡的一個個屬性對應用於參數,這樣子很好解決了這個問題,如:

 1 var f=function(args){ 2     sayHi(args.country || "Chinese", 3             args.name, 4             args.qq, 5             args.phone, 6             args.email)                 7 } 8 function sayHi(country,name,qq,phone,email){ 9     alert("Hi, I am a "+country+", my name is "+name+";qq:"+qq+";phone:"+phone+";email:"+email);    10 }11 f({name:"xiao",phone:"13888888888",email:"[email protected]",qq:123456});

 

這樣子只要把參數名記住了就可以了,不用管他的順序,同時給需要賦預設值得參數也很方便,如 args.country || "Chinese",如果調用函數的時候沒有給country 這一參數實參,那麼他的預設值就是chinese了。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.