在 Javascript 中定义一个带有命名空间的类
Define a class with namespace in Javascript
参考 https://stackoverflow.com/a/387733/418439,
// Define a class like this
function Person(name, gender){
// Add object properties like this
this.name = name;
this.gender = gender;
}
// Add methods like this. All Person objects will be able to invoke this
Person.prototype.speak = function(){
alert("Howdy, my name is" + this.name);
}
// Instantiate new objects with 'new'
var person = new Person("Bob", "M");
// Invoke methods like this
person.speak(); // alerts "Howdy, my name is Bob"
如何定义命名空间?
您可以创建一个包含所有类/函数的新对象:
var myNamespace = {};
myNamespace.Person = function (name, gender) {
// Add object properties like this
this.name = name;
this.gender = gender;
}
myNamespace.Person.prototype.speak = function() {
alert("Howdy, my name is" + this.name);
}
// Instantiate new objects with 'new'
var person = new myNamespace.Person("Bob", "M");
// Invoke methods like this
person.speak(); // alerts "Howdy, my name is Bob"
MDN 有一篇文章解释了 JavaScript 命名空间。
怎么样
var namespace = {};
namespace.Person = function(name, gender) { ... };
var myPerson = new namespace.Person();
检查这个参考:- 这里
var yourNamespace = {
foo: function() {
},
bar: function() {
}
};
...
yourNamespace.foo();
var MYNamespace = MYNamespace|| {};
MYNamespace.MyFirstClass = function (val) {
this.value = val;
this.getValue = function(){
return this.value;
};
}
var myFirstInstance = new MYNamespace.MyFirstClass(46);
alert(myFirstInstance.getValue());
JSFIDDLE: http://jsfiddle.net/rpaul/4dngxwb3/1/
我曾经制作了一个示例文件(供我自己使用(,所以我会在这里分享它,也许你会发现它很有用(警告:它包含的不仅仅是命名空间(:
//http://www.crockford.com/javascript/private.html
//http://www.dustindiaz.com/namespace-your-javascript/
//http://appendto.com/2010/10/how-good-c-habits-can-encourage-bad-javascript-habits-part-1/
//adding the whole shabang to a namespace
var NameSpace = (function (params)
{
//initialising constructor with parameter
//call as "var testObject = new MyConstructor("test");"
//then accessing the public members: "testObject.publicMember = 123;"
function MyConstructor(param, param2)
{
//initialising public instance member variables
//these could also be added by calling "testObject.[newMemberName] = [value];" to create a new property
//can be accessed by private and public methods
this.publicMember = param;
this.secondPublicMember;
//initialising private instance member variables
//private variables can only be added at creation time
//can be accessed by private methods, but not by the object's own public methods.
var privateMember = param2;
var secondPrivateMember;
//creates a private function, NOT accessible by public functions (ONLY by internal private and privileged ones)
//has access to all private/public functions and variables?
function PrivateFunction(params)
{
//place code here
//note this notation is short for "var PrivateFunction = function PrivateFunction(params) {};"
}
//creates a privileged function, accessible by all public (and private?) functions
//has access to all private/public functions and variables
this.PrivilegedFunction = function (params)
{
//place code here
};
}
//creating a public function, accessible by calling "testObject.PublicFunction(params)"
//can also be done by calling "testObject.[newFunctionName] = function (params) {};"
//has access to all public members and functions
MyConstructor.prototype.PublicFunction = function (params)
{
//place function code here
};
};
同样,这只是我使用顶部提到的链接为自己制作的模型。
相关文章:
- 在javascript中使用命名空间
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- 需要一个文件,然后使用一个函数,而不显式地将所需文件用作命名空间
- 从另一个命名空间重新定义(第三方)ClojureScript函数
- 如何为我的 class1 创建一个命名空间,然后将其实例化
- 从一个.js文件中调用另一个.js文件中的函数时出现命名空间问题
- 如何在同一个名称中创建两个不同的命名空间空间,避免覆盖第一个
- jQuery事件命名空间中所有事件的一个侦听器
- 如何创建一个空的命名空间对象而不覆盖另一个同名对象
- 将javascript命名空间添加到一个巨大的项目中
- 如何将一个可观察对象从一个命名空间的viewModel更新到另一个
- 在 Javascript 中定义一个带有命名空间的类
- 在node.js中创建一个带有命名空间的模块
- 重写并添加到我的命名空间一个javascript库
- 用Require.js和Backbone定义一个全局的应用命名空间
- Ember.js在一个命名空间中使用不同的资源
- 从字符串创建一个命名空间对象来调用函数
- 一个全局命名空间中的所有窗口变量
- 为什么不能在一个“命名空间”中使用函数?JavaScript文件填充下拉列表
- 一个函数模块的Typescript定义,它有命名空间