js 实现面向对象编程

参考链接  https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/create


js 没有类和实例的概念  但是还是可以实现面向对象编程

语法:

以一个对象为原型  创建继承的子对象的方法

Object.create(proto)

备注:

proto 是原型对象


eg;

var student = {
    name : 'student',
    height : 1.6,
    run : function () {
        console.log(this.name + ' is running ');
    }
};
function createStudent(name) {
    var proto = Object.create(student);

    proto.name = name;
    return proto;
}

var wang = createStudent('wang');

wang.run();

你可能感兴趣的:(js 实现面向对象编程)