JavaScript Tutorial/Object Oriented/Properties
Factory Paradigm
Factory functions create and return an object of a specific type.
<source lang="javascript">
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();</source>
Passing in of default values for the various properties in Object creation function
<source lang="javascript">
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();</source>
Properties of an object can be defined dynamically after its creation
<source lang="javascript">
var myHourse = new Object; myHourse.color = "red"; myHourse.doors = 4; myHourse.showColor = function () {
alert(this.color);
};</source>