本文實例講述了JavaScript的級聯函數用法。分享給大家供大家參考,具體如下:
級聯函數
級聯函數就是在對象調用中通過點的方式串聯調用,在jQuery中就是鏈式調用, 其關鍵點就是在內部 return this
返回自身
應用
function Person() { this.name = ''; this.age = 0; this.weight = 10;}Person.prototype = { setName:function(name){ this.name = name; return this; }, setAge:function(age){ this.age = age; return this; }, setWeight:function(weight) { this.weight = weight; return this; }}var p = new Person();p.setName('Joh').setAge(26).setWeight(80);console.log(p); // {name: "Joh", age: 26, weight: 80}
這里使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.VeVB.COm/code/HtmlJsRun測試上述代碼,可得如下運行結果:
更多關于JavaScript相關內容感興趣的讀者可查看本站專題:《JavaScript數據結構與算法技巧總結》、《JavaScript常用函數技巧匯總》、《JavaScript操作DOM技巧總結》、《JavaScript頁面元素操作技巧總結》及《JavaScript錯誤與調試技巧總結》
希望本文所述對大家JavaScript程序設計有所幫助。
新聞熱點
疑難解答