JavaScript中Function詳解_javascript技巧

來源:互聯網
上載者:User

關鍵字function用來定義函數。

複製代碼 代碼如下:

//函式宣告式定義:
function funcname([arg1[,args[...,argn]]]){
 statements
}
//函數運算式定義:
var funcname = function ([arg1[,args[...,argn]]]){
 statements
};

注意,function語句裡的花括弧是必需的,即使函數體只包含一條語句。

在JavaScript中,函數是Function類的具體執行個體。而且都與其它參考型別一樣具有屬性和方法。

函數名實際上是指向函數對象的指標,函數可以作為參數參與到傳參和傳回值中。

函數的對象特性

因為函數是Function的執行個體,而函數名僅僅是該執行個體的一個引用地址。因此可以作為參數和傳回值參與到函數的傳參過程中。

複製代碼 代碼如下:

function call_some_function(some_function, some_argument) {
    return some_function(some_argument);
}
function add_10(num) {
    return num + 10;
}
console.log(call_some_function(add_10,20)); //30

函數的內部屬性

arguments | this
•arguments對象中儲存著傳遞給函數的參數
•arguments.length返回傳入參數的個數
•Note: length屬性工作表示函數定義時候預設接收的參數數量。arguments.length表示函數實際執行時接收的參數數量。

複製代碼 代碼如下:

function test_arguments() {
    if (arguments.length == 2) {
        console.log(arguments.length);
        console.log(arguments);
    } else {
        console.log(arguments.length);
        console.log(arguments);
        arguments.callee(4, 5);
    };
}(1, 2, 3)
/**
 3
{ '0': 1, '1': 2, '2': 3 }
2
{ '0': 4, '1': 5 }
 **/

•arguments.callee()主要用在遞迴函式中調用函數自身的情境中。js和別的語言不同在於函數名只是一個指標,可以隨時變化,函數中利用函數名來調用自身屬於高耦合,可能會出現問題,而arguments.callee()調用自身就會規避掉這個問題

複製代碼 代碼如下:

function factorial(num) {
    if (num <= 1) {
        return 1;
    } else {
        return num * factorial(num - 1);
    };
}
function callee_f(num) {
    if (num <= 1) {
        return 1;
    } else {
        return num * arguments.callee(num - 1);
    };
}
factorial(10); //運行正常
f = factorial;
factorial = null;
f(10); //error
callee_f(10); //運行正常
f = callee_f;
callee_f = null;
f(10); //運行正常

•this主要用來協助函數引用函數所處範圍中的對象。

複製代碼 代碼如下:

var color = 'red';
function syaColor() {
    console.log(this.color);
}
syaColor(); //red
var o = new Object();
o.color = 'blue';
o.sayColor = sayColor;
o.sayColor(); //blue

call()和apply()

call()和apply()是每個函數都包含的自有方法。之前已經提到了函數是定義的對象,那麼調用函數時候,函數中的this是對當前與下變數的調用。而如果想改變函數執行所在域空間,則可以使用call()和apply()來實現。

複製代碼 代碼如下:

color = 'red';
var o = {color: 'blue'};
function sayColor() {
    console.log(this.color);
}
sayColor(); //red
sayColor.call(this); //red
sayColor.call(o); //blue

app()和call()的作用是相同的,區別主要在於傳入參數的不同。

call(this,para1,prar2,prar3) 第一個參數是函數要執行的範圍,後面的參數是函數的輸入參數,有多少個依次寫多少個。

apply(this,[para1,para2,prara3])第一個參數也是函數要執行的範圍,後面是一個Array的數組對象。

使用call()/apply()來擴充範圍最大的好處是對象和方法的解耦。

內建對象

Global對象可以理解成最外層的對象,所有的對象,以及不屬於其它對象的屬性和方法都被包含在Global對象中。
* isNaN(x) 用來檢查參數x是否為數字。如果為數字返回false,否則返回true
* isFinite(x) 用來檢查參數x是否為無窮大/小,如果是無窮大/小,則返回true
* parseInt(x) 用來解析字串並返回整數
* parseFloat(x) 用來解析字串並返回浮點數
* encodeURI()和encodeURIComponent()會對字串進行特殊的UTF-8編碼,規避一些特殊字元來讓瀏覽器能夠讀懂。他倆的區別主要在於encodeURI()不會對本身屬於URI的特殊字元進行編碼,而encodeURIComponent()會對其發現的所有非標準字元進行編碼。

複製代碼 代碼如下:

var uri = "http://www.wrox.com/illegal value.htm#start";
//http://www.wrox.com/illegal%20value.htm#start
console.log(encodeURI(uri))
//http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23start
console.log(encodeURIComponent(uri))

•對應的解碼函數為decodeURI()和decodeURIComponent()
•eval(script) 用來將script的內容在解譯器中執行並返回對應的結果。非常強大!

Note:在瀏覽器中,windows對象封裝了Global對象,並承擔了很多額外的任務和功能。

Math對象為另一個內建對象。為JavaScript提供了數學計算功能。

以上就是本文的全部內容了,希望小夥伴們能夠喜歡,能夠對大家有所協助。

聯繫我們

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