Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor or init function for an object

Tags:

javascript

oop

I was looking for a constructor or a init function for following situation:

var Abc = function(aProperty,bProperty){
   this.aProperty = aProperty;
   this.bProperty = bProperty;
}; 
Abc.prototype.init = function(){
   // Perform some operation
};

//Creating a new Abc object using Constructor.

var currentAbc = new Abc(obj,obj);

//currently I write this statement:
currentAbc.init();

Is there a way to call init function when new object is initialized?

like image 418
emphaticsunshine Avatar asked Feb 14 '12 03:02

emphaticsunshine


People also ask

Is a constructor function an object?

A constructor is a special function that creates and initializes an object instance of a class. In JavaScript, a constructor gets called when an object is created using the new keyword. The purpose of a constructor is to create a new object and set values for any existing object properties.

Is constructor the same as init?

But there's really two things going on when you call a constructor; a new object is created and then the constructor is called to initialise it. In C++/Java the "create a new object" part of that is invisible, whereas that can be exposed/customised in Python (via the __new__ method).

What is the constructor of an object?

Object() constructor The Object constructor creates an object wrapper for the given value. If the value is null or undefined , it will create and return an empty object. Otherwise, it will return an object of a Type that corresponds to the given value. If the value is an object already, it will return the value.

Which is called first init or constructor?

The constructor is called first or else there would be no object to call init() on.


2 Answers

You can just call init() from the constructor function

var Abc = function(aProperty,bProperty){
   this.aProperty = aProperty;
   this.bProperty = bProperty;
   this.init();
}; 

Here is a fiddle demonstrating: http://jsfiddle.net/CHvFk/

like image 62
Matt Greer Avatar answered Oct 22 '22 21:10

Matt Greer


Updated for 2020

While at the time of answering this questions classes were not widely available in JavaScript, this is no longer the case. Most major browsers now support the ES2015 class syntax, and with the prevalence of JavaScript transpilers providing backwards compatibility for those environments which don't support it, classes are now fairly safe to use and will look more natural to those coming to JavaScript from common OOP languages.

ES2015 Class version

class Abc {
  constructor (aProperty, bProperty) {
    this.aProperty = aProperty;
    this.bProperty = bProperty;

    this.init();
  }

  init () {
    // Initialization code here.
  }
}

let currentAbc = new Abc(obj, obj);

The private version is much the same as it was previously, since visibility keywords are not provided in the new class syntax

class Abc {
  constructor (aProperty, bProperty) {
    this.aProperty = aProperty;
    this.bProperty = bProperty;

    this.init = function () {
      // Initialization code here.
    }

    this.init();
  }
}

let currentAbc = new Abc(obj, obj);

There is also the option of creating the class in closure, which is what I believe some compilers may do to ensure that the function is private at runtime.

const Abc = (function() {
  function privateInit () {
    // Do initialization here
  }

  return class Abc {
    constructor (aProperty, bProperty) {
      this.aProperty = aProperty;
      this.bProperty = bProperty;

      privateInit.call(this);
    }
  };
})();

const currentAbc = new Abc(obj, obj);

If you're using a superset such as TypeScript, you can simply implement the init function privately, although this is only a compiler check, so it protects you from yourself, but not from external code.

class Abc {
  aProperty: any;
  bProperty: any;

  constructor (aProperty: any, bProperty: any) {
    this.aProperty = aProperty;
    this.bProperty = bProperty;

    this.init();
  }

  private init () {
    // Initialization code here.
  }
}

let currentAbc = new Abc(obj, obj);

Original Answer

Perhaps something like this?

var Abc = function(aProperty,bProperty){
    this.aProperty = aProperty;
    this.bProperty = bProperty;
    this.init = function(){
        // Do things here.
    }
    this.init();
}; 
var currentAbc = new Abc(obj,obj);
like image 21
James Hay Avatar answered Oct 22 '22 21:10

James Hay