JavaScript Tutorial/Object Oriented/Properties

Материал из Web эксперт
Перейти к: навигация, поиск

Factory Paradigm

Factory functions create and return an object of a specific type.



function createObject() {
    var bufObject = new Object;
    bufObject.color = "red";
    bufObject.doors = 4;
    bufObject.showColor = function () {
        alert(this.color)
    };
    return bufObject;
}
var myHourse1 = createObject();
var myHourse2 = createObject();


Passing in of default values for the various properties in Object creation function

function createObject(sColor, iDoors) {
    var bufObject = new Object;
    bufObject.color = sColor;
    bufObject.doors = iDoors;
    bufObject.showColor = function () {
        alert(this.color)
    };
    return bufObject;
}
var myHourse1 = createObject("red", 4);
var myHourse1 = createObject("blue", 3);
myHourse1.showColor();   
myHourse2.showColor();


Properties of an object can be defined dynamically after its creation

var myHourse = new Object;
myHourse.color = "red";
myHourse.doors = 4;
myHourse.showColor = function () {
    alert(this.color);
};