-1

I'm trying to do something really simple but not able to achieve this.

var Dummy = function() {
    self = this;
    setTimeout(function() {

        if(self.render) {
            self.render(); 
        }
    }, 10);
};
var test = new Dummy();
test.render = function() {
    console.log('Make constructor call this method? ');   
}

How can I make this work without settimeout?

Goal: Based on the function names attached to instance, I want to call them in particular order.

eg:

var Dummy = function() {
    if(function1) {
       function1();
    }
    if(function2) {
       function2();
    }
};
JS-JMS-WEB
  • 2,555
  • 3
  • 17
  • 26

3 Answers3

2

You question isn't entirely clear, but I assume you have a function that you want to have called as soon as your object has been instantiated. The simplest way to do this is to pass the function as an argument.

Something like:

var Dummy = function(myFunction) {
    self = this;
    self.render = myFunction;     // if you want to be able to call it again later
    self.render();                // if you want to call it now
};

var test = new Dummy(function() {
    console.log('Make constructor call this method? ');   
});

var anotherTest = new Dummy(function() {
    console.log("this instance will have a different render function");
});

Now, of course, this can be extended to handle any number of functions passed in:

var Dummy = function(func1, func2) {
   // Note: you may want to check the arguments actually are functions
   if (func1) {
       func1();
   }
   if (func2) {
       func2();
   }
}

var test = new Dummy(function() { console.log("1"); },
    function() { console.log("2"); });

Or you could pass in an array of functions in the order you want them executed:

var Dummy = function(funcArray) {
    for (var i=0; i < funcArray.length; i++) {
        funcArray[i]();
    }
}

var test = new Dummy([function() { console.log("1"); },
    function() { console.log("2"); }]);

Or you could pass in an object with the functions (and names) you want:

var Dummy = function(funcObject) {
    if (funcObject.func1) {
        funcObject.func1();
    }
    if (funcObject.func2) {
        funcObject.func2();
    }
}

var test = new Dummy({ func1 : function() { console.log("1"); },
    func2: function() { console.log("2"); });
Matt Burland
  • 44,552
  • 18
  • 99
  • 171
2

I am not sure what you are trying to do, but if what you want to do is call multiple functions (in order) when you instantiate an object instance of Dummy:

var Dummy = function() {
     for (var i = 0; i < arguments.length; i++) {
        if (typeof arguments[i] === "function") {
             arguments[i]();
        }
     }
}

var function1 = function() { console.log('Func 1 executed!'); }
var function2 = function() { console.log('Func 2 executed!'); }

var d = new Dummy(func1, func2, 'maybe a string also');

// log
'Func1 executed!'
'Func2 executed!'
Daniel Conde Marin
  • 7,588
  • 4
  • 35
  • 44
0

Becuase you are adding render() method on the Dummy instance (test) and you are not even calling that render() method.

I think you need to change to the following (assuming you don't want to use prototype to create the render() method):

var Dummy = function() {

    this.render = function() {
        console.log('Make constructor call this method called? ');   
    };

    self = this;

    setTimeout(function() {
        console.log(self);
        self.render(); 
    }, 10);
};
var test = new Dummy();
leo.fcx
  • 6,137
  • 2
  • 21
  • 37