創建對象和繼承

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();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章