javascript命名空间....

<!DOCTYPE html> 
<html>
	<head>	
	</head>
	<body>
 <script>
	function namespace(ns) {
		if(!ns || typeof ns !== "string" || !ns.length) return null;
		var steps = ns.split(".");
		var root = window;
		for ( var i = 0, len = steps.length; i < len; i++) {
			root[steps[i]] = root[steps[i]] || {};
			root = root[steps[i]];
		}
		return root;
	}
		namespace("www.com");
		www.com.say = function() {
			console.log("hello.js....");
		}
		www.com.say();

	/*
			//test code.....
			alert(window["a1"]);
			window["a1"] = {};
			var foo = (window["a1"]);
			foo["ff"] = {};
			alert(foo["ff"]);
	*/		

 </script>
		
	</body>
</html>

  

posted @ 2013-03-08 22:22  playerlife  阅读(230)  评论(0编辑  收藏  举报