35

I have an object written like this:

Object1.prototype = {
    isInit: false,
    Get : function (){}
} 

Now I'd like to add a constructor which takes one parameter. How can I do it?

5 Answers 5

66

Class declaration

var User = function(name, age) { // constructor
}

User.prototype = {}

Instance variables (members)

var User = function(name, age) {
    this.name = name;
    this.age = age;
}

User.prototype = {}

Static variables

var User = function(name, age) {
    this.name = name;
    this.age = age;
}

User.prototype = {
    staticVar: 15,
    anotherStaticVar: 'text'
}

Here I defined two static variables. Each User instance has access to these two variables. Note, that we can initialize it with value;

Instance functions (methods)

var User = function(name, age) {
    this.name = name;
    this.age = age;
}

User.prototype = {
    getName: function() {
        return this.name;
    },

    setName: function(name) {
        this.name = name;
    }
}

Usage example:

var user = new User('Mike', 29);
user.setName('John');
alert(user.getName()); //should be 'John'

Static functions

var User = function(name, age) {
    this.name = name;
    this.age = age;
}

User.create = function(name, age) {
    return new User(name, age);
}

User.prototype = {}
Sign up to request clarification or add additional context in comments.

3 Comments

That is not incorrect, but using a function expression with a name like that has various different problems in the various JavaScript interpreters and is probably (sadly) best avoided. Here is an article from kangax on the subject.
+1 But it is not really a class. JavaScript has no concept of classes.
@Felix: I agree with you about javascript classes conceprt, it just i call it 'class'.
10

Assuming that by "ctor" you mean "constructor", in JavaScript that's just a function. In this case your constructor would need to be "Object1" itself - in other words, what you've got there makes sense if you have already defined "Object1" to be a function.

Thus,

function Object1(param) {
  // constructor code
}

would be the constructor for your type.

Now there are some JavaScript libraries that provide a utility layer for defining classes. With those, you generally pass some sort of object (like you've got) that includes an "init" function. The libraries provide APIs for creating "classes" and for extending one class from another.

1 Comment

Note (1 Jan 2019) — this answer is from 2011, and predates the ES2015 class declaration mechanism. The answer is still accurate, but modern code may by preference prefer the newer syntax.
5

Javascript has prototype based object model. Check this mozilla wiki page and suddenly you'll feel much better in js land.

Comments

1

We can define a constructor in javaScript is same as we fine function, so constructor is just a function.

//function declaration
function func(){}

In case of Contructor We use initial letter in caps in construct like

//constructor 
function Func(){}

now do whatever you want to with your constructor

var constructor1 = new Func();

Comments

-4
class CLASS_NAME
{
   private:
         int variable;
   public:
          CLASS_NAME()    //constructor
          {
                 variable = 0;
          }
};

3 Comments

Muhammad welcome to SO. one line answers typically don't get to much attention around here. you could improve this answer by explaining what value it has for someone who is trying to figure out how to define a javascript constructor
Can you edit your proposed answer to expand on what this does and how it addresses the OP?
Dear I have given the basic example of constructor.

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.