user256034 user256034 - 6 months ago 8
Javascript Question

Javascript - How to define a Constructor

I have an object writen 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?

Answer

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 this 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 = {}
Comments