对象的作用域,对象方法扩展

来源:互联网 发布:yy挂机喊话软件 编辑:程序博客网 时间:2024/06/06 07:38
/**
 *
 * init: Function,
 * dashboard: Function,
 * controlPanel: Function,
 * appSetting: Function}}
 */
var myApp = {
date:{
name: "小明",
age: 24,
address: '不给你讲',
getName: function(){
return "参数里面的初始化方法。。。。。";
}
},
init: function(){
return "这是初始化执行的方法......" + this.date.name;
},
dashboard: function(){
return "仪表盘特性执行的功能......" + " --- " + myApp.controlPanel();
},
controlPanel: function(){
return "控制面板执行的功能......" + myApp.date.getName();
},
appSetting: function(){
return "通用属性的设置功能......";
}
};
/******************************************************************/




/*
一个简单的应用程序框架
先定义一个通用的对象,在里面放置一些通用的属性和方法
之后根据业务的需求扩展其方法
*/
var common = {
//初始化数据
initData:{
name: '小明',
age: 25,
address: '不告诉你'
},
method: function(){
return "对象的通用模块方法......";
}
};


//对象方法的扩展 dashboard
common.dashboard = {
init: function(){
return "dashboard模块的初始化方法...." + common.method();
},
update: function(){
return "dashboard模块的update方法...." + common.initData.name;
}
};


//对象方法的扩展 controlPanel
common.controlPanel = {
init: function(){
return "controlPanel模块的初始化方法.... " + common.dashboard.init();
},
update: function(){
return "controlPanel模块的update方法....";
}
};


//对象方法的扩展 controlPanel
common.appSeting = {
init: function(){
return "appSeting模块的初始化方法....";
},
update: function(){
return "appSeting模块的update方法....";
}
};
/******************************************************************/


/*
module模式
三个组件:
1、命名空间
2、一个立即执行的函数
3、函数的返回对象(返回对象包括公有方法和公有属性)
*/


//calculate为命名空间,传入$对象以缩短查找过程.
//一个简单的说明例子
var module = function($){


var message = "我是calculate中的私有属性";
function privateMethod(){
return "悄悄告诉你,我是私有方法.只能在calculate中进行调用";
};
//返回一些公有属性和方法
return {
init: function(){
return message + ",但是我通过返回值返回可看到";
},
prop: 24,
shareMessage: function(msg){
return "分享一些公共的数据信息---" + msg;
}
};
}(jQuery);


//进一步进行扩展
module.dashboard = function($){
//私有变量和方法(看成java类里面的私有变量)
var config = {
"color": "blue",
"title": "my dashboard",
"width": "960px"
};
//返回一些公有属性和方法(看成java类里面的公有方法)
return {
init: function(){
return "module.dashboard初始化方法。。。。。。" + config["color"];
},
update: function(){
return "appSeting模块的update方法....";
}
};
}(jQuery);
0 0