var msdnmagns = {};msdnmagns.pet = function(name) { // code here };msdnmagns.pet.prototype.tostring = function() { // code };var pet = new msdnmagns.pet(“yammer”);
命名空间的一个级别可能不是唯一的,因此可以创建嵌套的命名空间:
var msdnmagns = {};// nested namespace “examples”msdnmagns.examples = {};msdnmagns.examples.pet = function(name) { // code };msdnmagns.examples.pet.prototype.tostring = function() { // code };var pet = new msdnmagns.examples.pet(“yammer”);
可以想象,键入这些冗长的嵌套命名空间会让人很累。 幸运的是,库用户可以很容易地为命名空间指定更短的别名:
// msdnmagns.examples and pet definition...// think “using eg = msdnmagns.examples;”var eg = msdnmagns.examples;var pet = new eg.pet(“yammer”);alert(pet);
如果看一下 microsoft ajax 库的源代码,就会发现库的作者使用了类似的技术来实现命名空间,这里就不做详细说明了,有需要的小伙伴自己去度娘找吧。
以上就是本文的全部内容了,希望对大家学习javascript能够有所帮助
