标签:使用 io ar 数据 cti amp sp on new
//使用命名函数表达式实现递归
var factorial = (function f(num) {
if (num <= 1) {
return 1;
} else {
return num * f(num - 1);
}
});
//用作块级作用域(私有作用域)的匿名函数
(function(){
var now = new Date();
if (now.getMonth() == 0 && now.getDate() == 1) {
alert("Happy new year!");
}
})();
//使用构造函数模式实现特权方法的定义
function MyObject() {
var privateVariable = 10;//私有属性
function privateFunction() {//私有函数
return false;
}
//特权方法
this.publicMethod = function() {
}
}
//使用静态私有变量实现特权方法
(function(){
var privateVariable = 10;//私有属性
function privateFunction() {//私有函数
return false;
}
//MyObject初始化未经声明是全局变量
MyObject = function() {};
MyObject.prototype.publicMethod = function() {};
})();
/*
如果必须创建一个对象并以某些数据对其进行初始化,同时还要公开一下能够访问这些私有属性的方法,那么就可以使用模块模式。
*/
var application = function() {
var components = new Array();
components.push(new BaseComponent());
return {
getComponentCount : function() {
return components.length;
},
registerComponent : function(component) {
if (typeof component == "object") {
components.push(component);
}
}
};
}
递归、闭包、私有变量、特权方法、单例、模块模式(module pattern)
标签:使用 io ar 数据 cti amp sp on new
原文地址:http://my.oschina.net/quidditch/blog/307619