0

I've got a main JS script which looks something like this..

;(function ($) {

<main javascript>

}(jQuery));

I've just created my own external library script which looks like this..

;(function ($) {

var myClass = function() {
    <code>
};

}(jQuery));

What I want to do is pass my external library into the main JS script, to look something like this:

;(function ($, myClass) {

<main javascript>

}(jQuery, myClass));

However it says that myClass is undefined. How do I go about defining it?

(In terms of how I'm inserting the code it's using register/enqueue script with Wordpress. Both files are "active" so if I was to throw an alert into them both then they'd both get fired off, I'm just struggling with linking the two together so that I can use the scripts inside myClass whilst within my main JS file).

Thanks

user1157885
  • 1,999
  • 3
  • 23
  • 38

1 Answers1

2

myClass is defined within the scope of

(function ($) {
  ...
}(jQuery));

so unless the code in that library does

this.myClass = myClass;

or something else to explicitly export it into the global scope then there is no way to reference it from your main script.


How do I declare a namespace in JavaScript? has a lot of discussion on the pros & cons of various ways of exposing an interface to other code in JavaScript.

Community
  • 1
  • 1
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245