想要在命名空间内定义一个函数构造函数。到目前为止,我定义构造函数的方式是一个没有 NS 的简单构造函数,结合原型(prototype)继承。
代码看起来有点像:
function mySuperObject() {
var self = this;
self.p1 = "";
self.p2 = "jkejie";
// and others
}
mySuperObject.prototype.func1 = function(){...}
// and others
命名空间介绍:
在阅读了很多文章之后,我决定从一种非常简单的方式来定义命名空间,也许是最简单的方式。
基本上它只是定义一个指向对象文字的变量,内容是对象(上面代码片段中的“mySuperObject”)。构造函数如下:mySuperObjectInNS。
对象代码:
var MYNAMESPACE = {
//some variable outside the object
file : "ConstructorInNamespace_obj.js: ",
//Defining contructor function inside a namespace
mySuperObjectInNS : function(propOne, propTwo){
var self = this;
self.objectName = "mySuperObject";
self.propertyOne = propOne;
self.propertyTwo = propTwo;
self.doSomething = function(){
console.log(file + " doSomething called - function of object");
};
///many more functions and attributes
}
}
MYNAMESPACE.mySuperObjectInNS.prototype.specialFunction = function(){
console.log(file + " specialFunction called - prototypical inheritance defined in file of object, outside of namespace");
};
///many more functions and attributes
在另一个文件中可以实例化对象,如下所示:
...
var objOne = new MYNAMESPACE.mySuperObjectInNS("param11", "40");
//following line works just fine
objOne.doSomething();
....
问题:
我最近会遇到麻烦,我试图定义“私有(private)”
该对象的属性。这个对吗?
似乎它是另一回事,即使我可以从中实例化对象。
最佳答案
It seems to me that this all is about defining an Object-Literal and I will come into trouble the latest I am trying to define "private" properties of that object. Is this correct?
“私有(private)属性”与使用对象进行命名空间无关。事实上,最初在回答这个问题时,我将其读作“私有(private)函数”,因为 会是相关的。
在 JavaScript 中有很多方法可以实现私有(private)和半私有(private)属性,但它们与您如何创建构造函数以及它为对象提供的方法有关,而不是与您如何公开构造函数有关。 “命名空间”对象是关于如何公开构造函数的。
创建“私有(private)”属性的一种常见模式是定义需要在构造函数中访问它们的方法,并在构造函数中创建“属性”局部变量(因此它们根本不是真正的属性),如下所示:
function SuperObject() {
var privateInformation;
this.method = function() {
// This can access `privateInformation`, which is completely
// hidden from outside the constructor
};
}
如果您在“命名空间”模式中或单独执行此操作,则根本没有关系。
私有(private) 功能 ,另一方面,影响模式。我将在下面显示两者。
提供私有(private)函数的一个相当常见的变体是使用函数来创建对象,这也使您有机会创建私有(private)函数:
var TheNamespace = function() {
function privateFunction() {
}
function SuperObject() {
var privateInformation;
this.method = function() {
// This can access `privateInformation`, which is completely
// hidden from outside the constructor
};
}
SuperObject.prototype.otherMethod = function() {
// Can't access `privateInformation`, but has the advantage
// that the function is shared between instances
};
return {
SuperObject: SuperObject
};
}();
// usage
var s = new TheNamespace.SuperObject();
Is that mySuperObjectInNS still a constructor function? (For me it seems it is something else,even if I can intantiate objects from it.
是的。构造函数是希望您使用的任何函数
new
用它。Is is a very bad very bad way of namespacing or is kind of ok?
使用对象作为伪命名空间是常见的做法。您还可以考虑各种asynchronous module definition (AMD) 技术,这些技术在很大程度上使“命名空间”对象在很大程度上变得不必要。
回复您的评论:
You defined a self-invoking function....which returns an an object?
它不是一个自调用函数,它是一个内联调用函数,但是是的,它是一个返回对象的函数。
(if so I think parantheses are missing)
不,我们不需要任何不存在的括号,因为您在其他地方看到的外部括号的唯一原因是告诉解析器单词
function
开始一个表达式而不是声明;我们在上面不需要它,因为我们已经在赋值的右侧,所以在 function
时没有歧义。遇到。Due to you proposed this way, is it a better way to define the ns?
“更好”是一个主观术语。它为您提供了一个范围,您可以在其中定义您所询问的私有(private)函数。
Whereas I often also saw the option:
var = {} | someNSName;
What is this all about?
如果您有多个文件可以将内容添加到“命名空间”(这很常见),那么您经常会在每个文件中看到:
var TheNamespace = TheNamespace || {};
这样做是声明变量 如果 它之前没有声明过,并为其分配一个空对象如果 它还没有。在 第一个 被加载的文件,会发生这种情况:
var
被处理并创建一个新变量 TheNamespace
, 值为 undefined
. TheNameSpace = TheNameSpace || {}
分配已处理:自 undefined
是假的,curiously-powerful ||
operator产生新的 {}
,分配给 TheNamespace.
当下一个文件加载时,会发生这种情况:
var
是空操作,因为变量已经存在。 TheNameSpace = TheNameSpace || {}
分配已处理:自 TheNamespace
有一个非null
对象引用,真实而强大的||
运算符导致对对象 TheNamespace
的引用指。 也就是说,它根本没有效果。
使用它,您可以按任何顺序加载文件,或单独加载一个文件。
这是一个例子:
thingy.js
:var TheNamespace = TheNamespace || {};
TheNamespace.Nifty = function() {
function privateFunction() {
}
function Nifty() {
var privateInformation;
this.method = function() {
// Can access `privateInformation` here
};
}
Nifty.prototype.otherMethod = function() {
// ...
};
return Nifty;
}();
thingy.js
:var TheNamespace = TheNamespace || {};
TheNamespace.Thingy = function() {
function privateFunction() {
}
function Thingy() {
var privateInformation;
this.method = function() {
// Can access `privateInformation` here
};
}
Thingy.prototype.otherMethod = function() {
// ...
};
return Thingy;
}();
有拍品 该基本模式的变化,特别是如果一个文件可能会向
TheNamespace
添加多个内容.这是一个相当简洁地支持这样做的方法:var TheNamespace = function(exports) {
function privateFunction() {
}
function Nifty() {
var privateInformation;
this.method = function() {
// Can access `privateInformation` here
};
}
Nifty.prototype.otherMethod = function() {
// ...
};
exports.Nifty = Nifty;
function Thingy() {
var privateInformation;
this.method = function() {
// Can access `privateInformation` here
};
}
Thingy.prototype.otherMethod = function() {
// ...
};
exports.Thingy = Thingy;
}(TheNamespace || {});
关于javascript - 构造函数 - 在简单的命名空间 - Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30286933/