在Javascript中使用命名空间定义一个类

4
参考https://dev59.com/PXRC5IYBdhLWcg3wK92V#387733
// 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"

如何定义命名空间?

参考:JavaScript中的命名空间声明

2
JavaScript 中没有命名空间。 - idmean
1
@wumm 确实没有,但你可以模拟它们。 - Danilo Valente
可能是重复的问题:如何在JavaScript中声明命名空间? - Jay Harris
5个回答

6
您可以创建一个包含所有类/函数的新对象:
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命名空间的文章


2
yNamespace.Person = Person(name, gender) 必须改为 yNamespace.Person = function(name, gender) - idmean
@wumm 谢谢,我没看到那个。 - Danilo Valente

0

怎么样?

var namespace = {};

namespace.Person = function(name, gender) { ... };

var myPerson = new namespace.Person();

0

请查看此参考链接:这里

var yourNamespace = {

        foo: function() {
        },

        bar: function() {
        }
    };

    ...

    yourNamespace.foo();

你是从这里拿到这段代码的吗? - Danilo Valente

0
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/


-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
};

};

再次强调,这只是我使用顶部提到的链接制作的模型。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接