Javascript中如何引用指標

來源:互聯網
上載者:User

本文介紹Javascript中引用指標的方法。

請嘗試完成下列完形填空:

/* 建立一個隊列,頭為head0,尾為tail0 */function IntList(head0, tail0){    this.head = head0 || 0;    this.tail = tail0 || null;}/* 返回一個IntList包含數組中的所有數 */IntList.list = function(__args){    var sentinel = new IntList(),        len = __args.length,        p;    p = sentinel;    for(var i = 0; i < len; i++){        p.tail = new IntList(__args[i]);        p = p.tail;    }    return sentinel.tail;};/* 返回該對象的字串表示 */IntList.prototype.toString = function(){    var temp = "";    temp += "[";    for(var L = this; L !== null; L = L.tail){        temp = temp + " " + L.head;    }    temp += " ]";    return temp;};    /** 返回一個IntList,包含IntList A和IntList B, *  其中B的元素在A的後面。不能使用new關鍵字。 */function dcatenate(A, B){    /* 完成功能 */}    /** 返回一個新的IntList,其長度為len, *  以#start元素為開頭(其中#0是第一個元素), *  不能改變L。 */function sublist(L, start, len){    /* 完成功能 */}

這是一個用Javascript寫的鏈表題。由於鏈表擁有較為複雜的引用操作,正好可以用來考察下對Javascript的引用的理解。附帶簡單的測試案例:

/* 測試dcatenate和sublist函數是否正確 */function test(){    var A = IntList.list([4,6,7,3,8]),        B = IntList.list([3,2,5,9]);    dcatenate(A, B);    if(A.toString() === "[ 4 6 7 3 8 3 2 5 9 ]"){        alert("dcatenate函數正確。");    }else{        alert("dcatenate函數錯誤。");    }    var L = IntList.list([3,4,5,2,6,8,1,9]),        result = sublist(L, 3, 3);    if(result.toString() === "[ 2 6 8 ]"){        alert("sublist函數正確。");    }else{        alert("sublist函數正確。");    }}

聯繫我們

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