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

1 Answer 1

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.

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

1 Comment

@user1157885, please see my edit that includes links to relevant discussions.

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.