2017/4/24 阿里前端筆試題
阿新 • • 發佈:2019-01-03
題目要求:
1、Child繼承自Parent;
2、執行p.set(20).get()返回結果為20;執行p.set(30).get()返回結果為30;
3、執行c.set(30).get()返回結果為50,執行p.get()返回結果為20;
程式碼如下:
function Parent() { this.value = 20; } Parent.prototype = { constructor: Parent, set: function (num) { this.value = num; return this; }, get: function () { return this.value; } }; var p = new Parent(); console.log(p.set(20).get());//20 console.log(p.set(30).get());//30 function Child() { Parent.call(this);//繼承value屬性 //重寫set函式 this.set = function (num) { this.value = this.value + num; return this; } } Child.prototype = new Parent(); var c = new Child(); console.log(c.set(30).get());//50
題目的個人理解:題目裡2和3相互獨立,沒有執行先後的關係,否則如果先執行2再執行3,2裡先設定成20再設定成30,而3裡邊返回20,這樣設定就沒有意義了