继承

2016-12-20  本文已影响0人  GarenWang

问题

1.继承有什么作用?

2.有几种常见创建对象的方式? 举例说明?

3.下面两种写法有什么区别?

//方法1
function People(name, sex){
    this.name = name;
    this.sex = sex;
    this.printName = function(){
        console.log(this.name);
    }
}
var p1 = new People('饥人谷', 2)

//方法2
function Person(name, sex){
    this.name = name;
    this.sex = sex;
}

Person.prototype.printName = function(){
    console.log(this.name);
}
var p1 = new Person('若愚', 27);

4.Object.create有什么作用?兼容性如何?如何使用?

5.hasOwnProperty有什么作用? 如何使用?

6.实现Object.create的 polyfill

7.如下代码中call的作用是什么?

      function Person(name, sex){
        this.name = name;
        this.sex = sex;
    }
    function Male(name, sex, age){
        Person.call(this, name, sex);    //这里的 call 有什么作用
        this.age = age;
    }

8.补全代码,实现继承

    function Person(name, sex){
        this.name=name;
        this.sex=sex;
    }

    Person.prototype.getName = function(){
       return this.name
    };    

    function Male(name, sex, age){
       Person.call(this,name,sex)
       this.age=age
    }

    var _prototype=Object.create(Person.prototype);
    _prototype.constructor=Male;
    Male.prototype=_prototype;

    Male.prototype.printName=function(){
        console.log('name is '+this.name)
    }
    
    Male.prototype.getAge = function(){
        return this.age
    };

    var ruoyu = new Male('若愚', '男', 27);
    ruoyu.printName();//name is 若愚

代码题

实现如下dialog 弹窗功能, 参考效果

代码预览
效果预览 由于作业上传上去GitHub上有,班级预览项目没有?使用了jsbin预览

上一篇 下一篇

猜你喜欢

热点阅读