4

Refer to 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"

How to define namespace as well?

3
  • 2
    There are no namespaces in JavaScript. Commented Aug 13, 2014 at 12:08
  • 1
    @wumm There really aren't, but you can simulate them. Commented Aug 13, 2014 at 12:09
  • possible duplicate of How do I declare a namespace in JavaScript? Commented Dec 10, 2014 at 1:33

5 Answers 5

6

You can just create a new object that contains all your classes/functions:

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 has an article explaining JavaScript namespacing.

Sign up to request clarification or add additional context in comments.

2 Comments

yNamespace.Person = Person(name, gender){ must be yNamespace.Person = function(name, gender){
@wumm Thanks, didn't see that.
0

How about

var namespace = {};

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

var myPerson = new namespace.Person();

Comments

0

Check this ref:- here

var yourNamespace = {

        foo: function() {
        },

        bar: function() {
        }
    };

    ...

    yourNamespace.foo();

1 Comment

Did you take this code from here?
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/

Comments

-1

I once made an example file (for my own use), so I'll share that here, maybe you will find it useful (warning: it contains more than just namespaces):

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

};

Again, this was just a mockup I made for myself using the links mentioned at the top.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.