美文网首页
javascript的面向对象编程

javascript的面向对象编程

作者: 你好667 | 来源:发表于2017-08-25 10:31 被阅读0次

    原型

    类似与继承关系(只是变成了对象继承对象)

    prototype ==  "原型"
    
    var Person = {
        name: 'lin',
        height: 1.8,
        run: function () {
            console.log(this.name + ' is running...');
        }
    };
    
    var linxw = {
        name: 'linxw'
    };
    
    linxw.__proto__ = Person;
    
    linxw.name; // 'linxw'
    linxw.run(); // linxw is running.
    
    // 就像run这个方法是从person人哪里继承过来的
    
    1. JavaScript不区分类和实例的概念,而是通过原型(prototype)来实现面向对象编程
    2. JavaScript的原型链和Java的Class区别就在,它没有“Class”的概念,所有对象都是实例,所谓继承关系不过是把一个对象的原型指向另一个对象而已。
    3. 原型链的概念就像继承树一样。(自下而上)

    创建原型继承

    1. Object.create()
    // 原型对象:
    var Person = {
        name: 'Lin',
        height: 1.2,
        run: function () {
            console.log(this.name + ' is running...');
        }
    };
    
    function createStudent(name) {
        // 基于Person原型创建一个新对象:
        var s = Object.create(Person);
        // 初始化新对象:
        s.name = name;
        return s;
    }
    
    var linxw = createStudent('小明');
    linxw.run(); // 小明 is running...
    linxw.__proto__ === Person; // true
    
    1. new创建基于原型对象
    function Person(name) {
        this.name = name;
    }
    
    Person.prototype.hello = function () {
        console.log(this.name);
    };
    
    var lxw = new Person("linxw");
    lxw.hello();//linxw
    
    

    对象的构造函数

    通过 new 运算,调用函数返回一个对象,改变构造函数内this指针指向对象本身。(如果this不new的话就是返回undefined,非strict模式下this.name =name不报错,因为this绑定为window,于是无意间创建了全局变量name,并且返回undefined)

    function Person(name) {
        this.name = name;
        this.hello = function () {
            alert('Hello, ' + this.name + '!');
        }
    }
    
    var xiaoming = new Person('小明');
    xiaoming.name; // '小明'
    xiaoming.hello(); // Hello, 小明!
    
    //原型链
    // xiaoming ----> Person.prototype ----> Object.prototype ----> null
    

    真正的原型继承(扩展类【对象】)

    // 用于继承的函数
    function inherits(Child, Parent) {
        var F = function () {};
        F.prototype = Parent.prototype;
        Child.prototype = new F();
        Child.prototype.constructor = Child;
    }
    // 父类
    function Student(props) {
        this.name = props.name || 'Unnamed';
    }
    
    Student.prototype.hello = function () {
        alert('Hello, ' + this.name + '!');
    }
    // 子类
    function PrimaryStudent(props) {
        Student.call(this, props);
        this.grade = props.grade || 1;
    }
    
    // 实现原型继承链:
    inherits(PrimaryStudent, Student);
    
    // 绑定其他方法到PrimaryStudent原型:
    PrimaryStudent.prototype.getGrade = function () {
        return this.grade;
    };
    

    class继承

    function Student(name) {
        this.name = name;
    }
    
    Student.prototype.hello = function () {
        alert('Hello, ' + this.name + '!');
    }
    
    
    //上下文代码 等价
    class Student {
        constructor(name) {
            this.name = name;
        }
    
        hello() {
            alert('Hello, ' + this.name + '!');
        }
    }
    
    
    // 继承
    class PrimaryStudent extends Student {
        constructor(name, grade) {
            super(name); // 记得用super调用父类的构造方法!
            this.grade = grade;
        }
    
        myGrade() {
            alert('I am at grade ' + this.grade);
        }
    }
    

    相关文章

      网友评论

          本文标题:javascript的面向对象编程

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