2017/4/24 阿里前端笔试题

题目要求:

   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,这样设置就没有意义了

猜你喜欢

转载自blog.csdn.net/u013910340/article/details/70665258
今日推荐