JS面向对象、继承、ES6中class类

作者: Brighten_Sun | 来源:发表于2017-05-07 22:10 被阅读72次

    js中的this:
      当前触发事件的对象(元素)
      触发这个函数的对象(看上下文)
    js中怎么创造对象:
      var obj=new Object(); //空白的对象

    面向对象编程方法(1):

    function CreatePeople(name,age){
        var obj=new Object();  //准备空白的对象
        //添加属性
        obj.name=name;
        obj.age=age;
        //添加方法
        obj.showName=function(){
            return this.name
        };
        obj.pop=function(){
            return this.age
        };
        return obj
    };
    
    var p1=CreatePeople('boy','20');
    alert(p1.showName())
    

    面向对象编程方法(2):没有return哦

    function CreatePeople(name,age){
        //var obj = new Object();  准备材料
        //new  造一个空白的对象给this
        //添加属性     加工
        obj.name=name;
        obj.age=age;
        //添加方法
        obj.showName=function(){
            return this.name
        };
        obj.pop=function(){
            return this.age
        };
        //默认返回this
        //return obj  出厂
    };
    
    var p1=new CreatePeople('boy','20');
    alert(p1.showName())
    

    面向对象编程方法(3):prototype是原型

    function Person(name,age){
        this.name=name;
        this.age=age;
    }
    Person.prototype.showName=function(){
        return alert('我叫'+this.name)
    };
    Person.prototype.showAge=function(){
        return alert('我的年龄是'+this.age)
    };
    var p1=new Person('boy','20');
    p1.showName();
    

    面向对象编程方法(3):属性的继承

    可以用call和apply的方法来继承,第一个参数都是纠正this的指向,后面是参数,两者的区别是apply传参可以用数组,想到了什么,arguments对吧,所有哪个方便,你知道的。
    fn.call(this的指向,参数1,参数2.....);
    fn.apply(this的指向,[参数1,参数2.....]);
    这其中我们要改变this的指向,通过call和apply来继承属性,然后把this的指向转向当前这个对象。

    function Student (name,id){
         Person.call(this,name); //第一个值改变this指向,第二个值是继承父级的参数
         Person.apply(this,[...args]);//第一个值改变this指向,第二个值是继承父级的参数
    
         this.id = id;
         this.showId = function(){
              alert('Good morning,Sir,My work number is '+this.id);
         }
    }
    var simon = new Student ('Simon',9527);
    

    面向对象编程方法(4):ES6版面向对象

     class Person{
        constructor(name,age){  //直接写属性
            this.name=name;
            this.age=age;
        }
        showName(){  //直接可以写方法
            return this.name
        }
        showAge(){
            return this.age
        }
    }
    
    //var xiaoming=new Person('小明','16');
    

    面向对象编程方法(4):ES6版继承

    class Student extends Person{ 
        constructor(name,age,job='student'){
            super(name,age); //直接就继承了属性和方法
            this.job=job;
        }
        showJob(){
            return this.job
        }
    }
    
    var xiaohua=new Student('小花','39');
    var xiaolv=new Student('小绿','45','student');
    alert(xiaohua.showJob();
    

    岔开:说一下this的问题

    this:谁调用他,或者这个调用属于谁,包一层的时候this会变。

    this的优先级:
      new -> object
      定时器 -> window
      事件 -> 触发事件的对象
      方法 -> 方法属于谁
      其他/直接调用的

    方法的继承:

    方法1:
        Student.prototype=Person.prototype;
        //问题:发生了引用,父级也有子级的方法
      
    方法2:
        for(var name in Person.prototype){
            Student.prototype[name]=Person.prototype[name];
        }
        //问题:alert(Student instanceof Person);
        //学生不是人,创造出来的对象不属于构造函数了
    
    方法3:
        Student.prototype=new Person();
        //问题:不认亲爹了
        //解决: Student.prototype.constructor=Student; //让构造函数直接等于他自己。
    

    instanceof 一个对象属于某个类 obj instanceof Object
    constructor 检测某个对象是什么构造出来的 如:oDate.constructor==Date;

    相关文章

      网友评论

        本文标题:JS面向对象、继承、ES6中class类

        本文链接:https://www.haomeiwen.com/subject/vgujtxtx.html