<script>
var myApp = {};//通过全局变量来实现命名空间
maApp.Parent = function (){
};
myApp.Child = function(){
};
</script>
<script>
//不安全代码
var myApp = {};
//安全代码
if(typeof myApp === "undefined"){
var myApp = {};
}
//或者使用更短的语句
var myApp = myApp || {};
</script>
<script>
myApp.namespace("myApp.modules.module2");
//等同于如下代码
var myApp = {
modules : {
module2:{}
}
};
</script>
<script>
var myApp = myApp || {};
myApp.namespace = function(ns_string){
var parts = ns_string.split(‘.‘), parent = myApp, i;
if(parent[0] === "myApp"){
parts = parts.slice(1);
}
for(i = 0; i < parts.length; i++){
if(typeof parent[parts[i]] === "undefined"){
parent = parent[parts[i]];
}
}
return parent;
};
var modules2 = myApp.namespace("myApp.modules.module2");
console.log(modules2 === myApp.modules.module2);
myApp.namespace("modules.module2");
</script>
var myFunction = function(){ var event = YAHOO.util.event, dom = YAHOO.util.Dom; }
var myObj = {
myProp : 1,
getPro: function(){
return this.myProp;
}
};
console.log(myObj.myProp);
console.log(myObj.getPro());
<script>
function Gadget(){
//私有成员
var name = "iPoid";
//共有函数
this.getName = function(){
return name;
};
}
var toy = new Gadget();
console.log(toy.name);//undefined
console.log(toy.getName);//iPoid
</script>
<script>
function Gadget(){
//私有成员
var specs = {
screen_width: 320,
screen_hight: 650,
color: ‘RED‘
};
//共有函数
this.getSpecs = function(){
return specs;
};
}
var toy = new Gadget(),
specs = toy.getSpecs();
specs.color = "GUESS";
console.log(specs.screen_width);//320
console.log(specs.color);//GUESS
console.dir(toy.getSpecs());
</script>
<script>
var myObj = (function(){
var name = "name";
return {
getName: function(){
return name;
}
};
}());
console.log(myObj.getName());//name
</script>
<script>
function Gadget(){
var name = "Ipod";
this.getName = function(){
return name;
};
}
Gadget.prototype = (function(){
var browser = "others";
return{
getBrowser: function(){
return browser;
}
};
}());
var toy = new Gadget();
console.log(toy.getName());//name
console.log(toy.getBrowser());//others
</script>
<script>
var myArray;
(function(){
var astr = "[object Array]",
toString = Object.prototype.toString;
function isArray(s){
return toString.call(s) === astr;
}
function indexOf(haystack, needle){
var i = 0,
max = haystack.length;
for(; i < max; i+= 1){
if(haystack[i] === needle){
return i;
}
}
return -1;
}
myArray = {isArray: isArray,
indexOf: indexOf,
inArray: indexOf};
console.log(myArray.isArray([1, 2]));//true
console.log(myArray.isArray({0 : 1}));//false
console.log(myArray.indexOf(["a", "b"], "a"));//0
console.log(myArray.indexOf(["a", "b"], "d"));//-1
}());
console.log(myArray.indexOf(["a", "b"], "a"));//0
console.log(myArray.indexOf(["a", "b"], "d"));//-1
</script>
<script>
myApp.namespace("myApp.util.array");
myApp.util.array = (function(){
var uobj = myApp.util.object,
ulang = myApp.util.lang,
array_string = "[object Array]",
ops = Object.prototype.toString;
return {
inArray: function(needle, haystack){
for(var i = 0, max = haystack.length; i < max; i+= 1){
if(haystack[i] === needle){
return true;
}
}
},
isArray: function(s){
return ops.call(a) === array_string;
}
};
}());
</script>
<script>
myApp.namespace("myApp.util.array");
myApp.util.array = (function(){
var uobj = myApp.util.object,
ulang = myApp.util.lang,
array_string = "[object Array]",
ops = Object.prototype.toString;
inArray: function(needle, haystack){
for(var i = 0, max = haystack.length; i < max; i+= 1){
if(haystack[i] === needle){
return true;
}
}
},
isArray: function(s){
return ops.call(a) === array_string;
};
return {
isArray: isArray,
indexOf: inArray
};
}());
</script>
<script>
myApp.namespace("myApp.util.array");
myApp.util.array = (function(){
var uobj = myApp.util.object,
ulang = myApp.util.lang;
Constr;
Constr = function(o){
this.elements = this.toArray(o);
};
//共有API原型
Constr.prototype = {
constructor: myApp.util.array,
version: "2.0",
toArray: function(obj){
for(var i = 0, a= [], len = obj.length; i < len; i+= 1){
a[i] = obj[i];
}
return a;
}
};
//返回要分配给新命名空间的构造函数
return Constr;
});
var arr = new myApp.util.array([obj]);
</script>
<script>
MYAPP.util.module = (function(app, global){
//
}(MYAPP, this));
</script>
原文:http://blog.csdn.net/mergades/article/details/41350127