javascript函數,javascript

來源:互聯網
上載者:User

javascript函數,javascript
javascript函數
function cal(x){
return 2*x;
}
console.log(cal(10));
var x=2;
function cal(){
x=x*2;
}
function cal(x){
return 2*x;
}
console.log(cal());
//undefined
console.log(cal(10));
如果一個函數沒有特定的返回值,那麼它將返回一個undefined
javascript中的函數是第一類(first-class)的對象,著意味著可以向處理其他對象一樣處理函數
可以將一個函數賦予一個變數,或者儲存在另外一個資料結構中(一個數組或者對象)可以將函數作為參數
傳遞給其他函數,可以將函數作為另一個函數的返回值,函數還可以採用匿名函數的方式,即根本沒有綁定與
函數名標識符的函數
var calc=function(x){
return x*2;
}
calc(2);
//4
var calc=function(x){
return x*2;
}
var x=calc(2);
console.log(x);
//4
對於jQuery開發人員,第一類函數的兩個特殊是非常的重要
(1)將函數作為參數傳遞給其他函數
(2)匿名函數
function reporter(fn){
console.log(fn());
}
reporter(function(){});
//undefined
reporter(function(){return 'string'})
//string
function cal(){
return 4*2;
}
reporter(cal);
//8

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.