JavaScript Tutorial/Object Oriented/Properties — различия между версиями

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

Текущая версия на 08:24, 26 мая 2010

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);
};