js创建对象方式与集成方式

创建对象

  1. 工厂模式
    没有解决对象识别

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    function createPerson(name, age, job){
    var o = new Object();
    o.name = name;
    o.age = age;
    o.job = job;
    o.sayName = function(){
    alert(this.name)
    }
    return o
    }
    var person1 = createPerson('tom', 29, 'web开发')
  2. 构造函数模式
    方法重复定义

    1
    2
    3
    4
    5
    6
    7
    8
    9
    function CreatePerson(name, age, job){
    this.name = name;
    this.age = age;
    this.job = job;
    this.sayName = function(){
    alert(this.name)
    }
    }
    var person1 = new CreatePerson('tom', 29, 'web开发')
  3. 原型模式
    所有的实例有了相同的属性和方法

    1
    2
    3
    4
    5
    6
    7
    8
    function Person(){}
    Person.prototype.name = 'tom';
    Person.prototype.age = 29;
    Person.prototype.job = 'web开发';
    Person.prototype.sayName = function(){
    alert(this.name)
    }
    var person1 = new Person()
  4. 原型模式 + 构造函数模式
    所有的实例有了相同的属性和方法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    function Person(name, age, job){
    this.name = name;
    this.age = age;
    this.job = job;
    }

    Person.prototype.sayName = function(){
    alert(this.name)
    }
    var person1 = new Person()
  5. 寄生构造函数模式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    function SpecialArray(){
    var values = new Array();
    values.push.apply(values, arguments);
    values.toPipedString = function(){
    return this.join("|");
    }
    return values
    }
    var colors = new SpecialArray('red', 'blue', 'green');
    alert(colors.toPipedString())
  6. 稳妥构造函数模式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    function Person(name, age, job){
    var o = new Object();
    o.sayName = function(){
    alert(name)
    };
    return o
    }
    var friend = Person('tom', 29, 'web开发');
    friend.sayName();

继承

  1. 原型链
    子类构造函数原型指向父类的实例,缺陷在于创建子类的时候,不能像父类的构造函数传递参数,而且原型中包含引用类型的值,修改后会影响所有的实例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    function SuperType(){
    this.property = true
    }
    SuperType.prototype.getSuperValue = function(){
    return this.property;
    }
    function SubType(){
    this.subpreoperty = false;
    }
    SubType.prototype = new SuperType();
    SubType.prototype.getSubValue = function(){
    return this.subproperty;
    }
    var instance = new SubType();
    alert(instance.getSuperValue())
  2. 借用构造函数继承
    在子类的构造函数中,使用call方法调用父类函数,生成实例,缺陷:只能继承到父类构造函数中的属性方法,原型上无法获取,并且,方法不能服用

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    function SuperType(){
    this.colors = ['red', 'blue', 'greem']
    }
    function SubType(){
    SuperType.call(this)
    }
    var instance1 = new SubType();
    instance1.colors.push('black');
    alert(instance1.colors);
    var instance2 = new SubType();
    alert(instance2.colors)
  3. 组合继承
    使用call继承父类的属性(可以传参,自定义)和方法,再修改子类原型指向父类实例继承父类原型的方法,缺陷:父类的构造函数执行了两次

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    function SuperType(name){
    this.name = name
    this.colors = ['red', 'blue', 'greem']
    }
    SuperType.prototype.sayName = function(){
    alert(this.name)
    }
    function SubType(name, age){
    SuperType.call(this, name);
    this.age = age
    }
    SubType.prototype = new SuperType();
    SubType.prototype.sayAge = function(){
    alert(this.name)
    }
  4. 原型式继承

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    function object(o){
    function F(){}
    F.prototype = o;
    return new F();
    }
    var person = {
    name: 'tom',
    friends: ['shelby', 'jerry']
    }
    var anotherPerson = object(person);
    anotherPerson.name = 'Linda';
    anotherPerson.friends.push('bob');
    var anotherPerson2 = object(person);
    anotherPerson2.name = 'Grey';
    anotherPerson2.friends.push('barbie')
    alert(person.friends)
  5. 寄生式继承

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    function object(o){
    function F(){}
    F.prototype = o;
    return new F();
    }
    function createAnother(original){
    var clone = object(original);
    clone.sayHi = function(){
    alert('hi')
    }
    return clone
    }
    var person = {
    name: 'tom',
    age: 29
    }
    var anotherPerson = createAnother(person);
    anotherPerson.sayHi();
  6. 寄生组合式继承
    通过构造函数继承属性,原型链方式继承方法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    function object(o){
    function F(){}
    F.prototype = o;
    return new F();
    }
    function inheritPrototype(subType, superType){
    var prototype = object(superType.prototype);
    prorotype.constructor = subType;
    subType.prototype = prototype
    }
    function Super() {}

    借助Object.create方法实现如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    Super.prototype.getNumber = function() {
    return 1
    };
    function Sub() {}
    let s = new Sub();
    Sub.prototype = Object.create(Super.prototype, {
    constructor: {
    value: Sub,
    enumerable: false,
    writable: true,
    configurable: true
    }
    })