创建对象和继承

1.创建对象

//工厂模式---无法识别对象,无法知道对象类型
function createPerson(name, age) {
    var person = new Object();
    person.name = name;
    person.age = age;

    person.sayName = function () {
        alert(this.name);
    }

    return person;
}

//构造函数模式---sayName是不同对象的实例(抽出来作为全局函数,但是破坏封装性)
function Person(name, age) {
    this.name = name;
    this.age = age;

    this.sayName = function () {
        alert(this.name);
    }
}

function prototype() {
    //原型模式---引用类型的数据会出错
    function Person() {
    }

    Person.prototype = {
        constructor: Person,//重写默认的constructor使其指向Person
        name: "json",
        age: 23,
        sayName: function () {
            alert(this.name);
        }
    };

    var p1 = new Person();
    p1.sayName();
}

//构造函数模式和原型模式混合---最常用
function standard() {
    function Person(name, age) {
        this.name = name;
        this.age = age;
        this.friends = ["kitty", "jack"];
    }

    Person.prototype = {
        constructor: Person,//重写默认的constructor使其指向Person
        sayName: function () {
            alert(this.name);
        }
    };

    var p1 = new Person("jack", 11);
    var p2 = new Person("rose", 22);

    p1.friends.push("hhh");

    alert(p1.friends);
    alert(p2.friends);
}

2.继承

//原型链:利用原型让一个引用类型继承另一个引用类型的属性和方法---引用类型会出问题
function prototype() {
    function SuperType() {
        this.property = true;
    }

    SuperType.prototype.getSuperValue = function () {
        return this.property;
    }

    function SubType() {
        this.subproperty = false;
    }

    SubType.prototype = new SuperType();

    SubType.prototype.getSubValue = function () {
        return this.subproperty;
    }

    var instance = new SubType();
    alert(instance.getSuperValue());
    alert(instance.getSubValue());
}

//借用构造函数---优势:子类向超类传参;劣势:没法函数复用
function constructureStealing() {
    function SuperType() {
        this.colors = ["red", "blue"];
    }

    function SubType() {
        SuperType.call(this);
    }

    var instance = new SubType();
    instance.colors.push("green");
    alert(instance.colors);

    var instance2 = new SubType();
    alert(instance2.colors);
}

//原型链实现属性和方法的继承,借用构造函数实现实例属性的继承
function mix() {
    function SuperType(name) {
        this.name = name;
        this.colors = ["red", "blue"];
    }

    SuperType.prototype.sayName = function () {
        alert(this.name);
    }

    function SubType(name, age) {
        //继承属性
        SuperType.call(this, name);

        this.age = age;
    }

    //继承方法
    SubType.prototype = new SuperType();
    SubType.prototype.constructor = SubType;
    SubType.prototype.sayAge = function () {
        alert(this.age);
    }

    var instance = new SubType("jack", 29);
    instance.colors.push("green");
    alert(instance.colors);
    instance.sayName();
    instance.sayAge();

    var instance2 = new SubType("rose", 20);
    alert(instance2.colors);
    instance2.sayName();
    instance2.sayAge();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章