javascript函數
來源:程序員人生 發布時間:2014-12-17 08:54:10 閱讀次數:2684次
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));
如果1個函數沒有特定的返回值,那末它將返回1個undefined
javascript中的函數是第1類(first-class)的對象,著意味著可以向處理其他對象1樣處理函數
可以將1個函數賦予1個變量,或保存在另外1個數據結構中(1個數組或對象)可以將函數作為參數
傳遞給其他函數,可以將函數作為另外一個函數的返回值,函數還可以采取匿名函數的方式,即根本沒有綁定與
函數名標識符的函數
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類函數的兩個特殊是非常的重要
(1)將函數作為參數傳遞給其他函數
(2)匿名函數
function reporter(fn){
console.log(fn());
}
reporter(function(){});
//undefined
reporter(function(){return 'string'})
//string
function cal(){
return 4*2;
}
reporter(cal);
//8
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈