Function - Javascript - Correct way to create object and pass as argument

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

   This question already has an answer here:
    * /questions/20279484/how-to-access-the-correct-this-inside-a-callback /questions/20279484/how-to-access-the-correct-this-inside-a-callback 4 answers

Answers

you have lot s of error s in your code,I think this is what you trying to do

      function timer(func){

           func();
        }

//       following your constructor  
        function doSomething(){
          var outer = this; // this refer  to current object 
           this.execute = function(){
              alert (outer.param1 + " - " +outer.param2); // its a closure function  
           };

          return this;
        }

        var instance = new doSomething();
        instance.param1 = "Hi";
        instance.param2 = "Test";
        timer(instance.execute);

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/32139324/javascript-correct-way-to-create-object-and-pass-as-argument

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils