思路:
声明对象有两种常见的方式:var obj = {};和var obj = new Object();。前面一种可以直接在括号中以key:value的方式定义属性,后一种采用点运算符给对象添加属性。
字面量模式:
function createModule(str1, str2) {
var obj={
greeting:str1,
name:str2,
sayIt : function(){return this.greeting + ", " + this.name;}
};
return obj;
}
构造函数模式:
function createModule(str1, str2) {
function Obj()
{
this.greeting = str1;
this.name = str2;
this.sayIt = function(){return this.greeting + ", " + this.name;}
}
return new Obj();
}
创建对象模式:
function createModule(str1, str2) {
function CreateObj()
{
obj = new Object;
obj.greeting = str1;
obj.name = str2;
obj.sayIt = function(){return this.greeting + ", " + this.name;}
return obj;
}
return CreateObj();
}