本文實例講述了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}
可得如下運行結果:
希望本文所述對大家JavaScript程序設計有所幫助。
新聞熱點
疑難解答