12

I want to create an object and run two of its methods on object creation. So if my object is

function newObj(){
this.v1 = 10;
this.v2 = 20;
this.func1 = function(){ ....};
this.func2 = function(){...};
}

and the call to the object is

var temp = new newObj();

I want to run func1() and func2() without calling them explicity on temp variable, like temp.func1(). I want them to be called when I create the new Object variable. I tried putting this.func1() inside the newObj declaration but it doesn't seem to work.

6 Answers 6

11

Add method invocation statements in constructor:

function newObj(){
this.v1 = 10;
this.v2 = 20;
this.func1 = function(){ ....};
this.func2 = function(){...};
this.func1();
this.func2();
}

I think it is solution of your needs.

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

Comments

6

Just call it from within the constructor itself it works just fine: http://jsfiddle.net/yahavbr/tTf9d/

The code is:

function newObj(){
    this.v1 = 10;
    this.v2 = 20;
    this.func1 = function() { alert("func1"); };
    this.func2 = function() { alert("func2"); };

    this.func1();
    this.func2();
}

Comments

4

This works for me in Chrome:

function newObj(){
  this.v1 = 10;
  this.v2 = 20;
  this.func1 = function(){ this.v1 += 1; };
  this.func2 = function(){ alert(this.v1); };
  this.func1();
  this.func2();
}
var obj = new newObj();

Comments

1

Try wrapping it in an self invoking function if you never plan on reusing it, like this:

function newObj(){
    this.v1 = 10;
    this.v2 = 20;
    this.func1val = (function(){ alert('called from c\'tor'); })();
    this.func2val = (function(){ return 2 + 1; })();
}

var temp = new newObj();
alert('temp.func2val = ' + temp.func2val);

DEMO

2 Comments

Thanks, this worked. The this.func1() doesn't seem to work in Chrome for me.
function highLightElement(element) { this.LEVEL = 128; this.ELEMENT = element; this.INTERVAL = 100; this.setTimeout(increaseYellow, INTERVAL); this.setYellowLevel = (function() { var hex = this.LEVEL.toString(16); element.style.backgroundColor = '#ffff' + hex; })(); this.newfunc = (function(){ setTimeout(increaseYellow, INTERVAL);})(); this.increaseYellow = function(){ LEVEL += 10; if (LEVEL > 255) { LEVEL = 255; } setYellowLevel(ELEMENT, LEVEL); if (LEVEL < 255) { setTimeout(increaseYellow, INTERVAL); } }; }
0

Using Self invoking function we can call and we can also share parent parameter by doing some work around public variable var that = this;

function newObj(){

this.v1 = 10; // public variable
this.v2 = 20; // public variable
var that = this;  // Allow access to parent function variable to inner function
   (function(){
     // access parent function variable
     // using 'that' ex: 
       that.v1 = 50;
     //fun1code stuff 
   })();

   (function(){
     // access parent function variable
     // using 'that' ex: 
     that.v2 = 60;
     //fun2code stuff 
   })();
}

var temp = new newObj();
console.log(temp.v1);  // output 50
console.log(temp.v2);  // output 60

Comments

0

I think perhaps it needs to be stresed that in JavaScript you need to define the object's functions (or methods, if you prefer that term) before you call them.

For example, if you want to call this.func1() upon instantiation:

var new_object = new newObj();  // create/instantiate an object

function newObj(){
  this.v1 = 10;
  this.v2 = 20;

  this.func1();  //  <-- calling it here causes an error

  this.func1 = function(){ ....};
  this.func2 = function(){ ....};
  this.func3 = function(){ ....};
}

TypeError: this.func1 is not a function

This is a problem I came across years ago when trying to understand how to do OOP in JS. Because in other languages like Java or PHP, you have a constructor function/method usually at the top of your class, and beneath you write in your other functions/methods.

So it would seem logical to write your class thus: 1) define your object's properties, and then 2) list the things you want to do when the object is instantiated, and then 3) list the other class functions/methods.

BUT NO!!

With JavaScript, you must define the object's functions before you call them.

So if you want to call two methods on object creation/instantiation, lets say this.func1() and this.func2(), first define everything in your class and at the end place your method calls:

var new_object = new newObj();  // create/instantiate an object

function newObj(){
  this.v1 = 10;
  this.v2 = 20;

  this.func1 = function(){ ....};
  this.func2 = function(){ ....};
  this.func3 = function(){ ....};

  this.func1();  //  <-- it works here!
  this.func2();  //  <-- it works here!
}

If you wanted to have your code organised with a constructor method placed at the top of other class methods (like previously mentioned, how PHP and Java do it) then you could make a little this._constructor() method and place things there, and call it at the end of your class:

function newObj(){
  this.v1 = 10;
  this.v2 = 20;

  this._constructor = function(){  // do constructor things here
    this.func1();
    this.func2();
  }
  this.func1 = function(){ ....};
  this.func2 = function(){ ....};
  this.func3 = function(){ ....};

  this._constructor();  // call just one method here, nice and tidy
}

Some may say it's kinda redundant, but whatever helps to make your workflow faster... :)

Just for the record, if you want to pass some argument when creating/instantiating an object, say you wanted to have the option to set this.v1 then you could do it like this:

function newObj(set_v1){
  this.v1 = 10;
  this.v2 = 20;

  this._constructor = function(set_v1){  // do constructor things here
    if ( set_v1 != undefined ){  // you can come up with a better condition here
      this.v1 = set_v1;
    }
    this.func1();
    this.func2();
  }
  this.func1 = function(){ ....};
  this.func2 = function(){ ....};
  this.func3 = function(){ ....};

  this._constructor(set_v1);  // call the constructor here and pass the argument
}

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.