-2

I need to modify the self invoked javascript function from outside of this function.
I have counter in the self invoked function, it is working fine in the plugin. But when i need to change the counter from out side. it is not able to work.

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
junaid TK
  • 51
  • 8
  • 3
    Welcome to Stack Overflow! Please visit the [help], take the [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output. – mplungjan Sep 26 '18 at 11:56
  • Related: https://stackoverflow.com/questions/111102/how-do-javascript-closures-work – T.J. Crowder Sep 26 '18 at 11:56
  • 2
    the code please... – Aurangzeb Sep 26 '18 at 11:57
  • (function( $ ) { 'use strict'; var counter = 1; })( jQuery ); I need to modify the counter from outside of this script – junaid TK Sep 26 '18 at 12:19

2 Answers2

0

Changing function properties from outside might be doable, but it's pretty much goes agains the idea of functions.

In my opinion - unless you will publish your code so say otherwise - you might be attempting the solution from the wrong direction.

Try to share the problem you are trying to solve, and not the solution you cant implement.

Happy Coding!

Itamar
  • 1,601
  • 1
  • 10
  • 23
0

i have assigned the self invoked function to a variable and set some return value from the function.

var assigned_variable = (function( $ ) {

  function test_function(counter){
    "add your code for changing the counter"
  }

  return {
    test_function : test_function,
  }

})(jQuery);

function Innerfunction(counter){
  assigned_variable.test_function(counter);
}

Now we can access the Innerfunction from outside of the file. We can predefine the required changes in the "test_function" for the variable counter

junaid TK
  • 51
  • 8