0

I have a huge script with messy dependencies to other js files. My problem is, I need to include the script on various pages but not the other js files. and it stucks when some of variables are there which are defined in other files, I can handle them one by one but just have not enough time to do so.

I want to know is there any mechanism just like php autoloaders in javascript so whenever it gets something undefined, I can hook a chunk of code to handle that.

1 Answer 1

1

This is probably not the actual concrete answer you're looking for, but it seems to me like you'd be better of spending some time refactoring the code, and in the process of that you might want to look at some modular JavaScript patterns. I myself use RequireJS to handle dependencies etc.

I would recommend reading the following article: Writing Modular JavaScript With AMD, CommonJS & ES Harmony

I know you said that you haven't got the time, but consider taking the time anyways - it will help you to have the architecture in place when needing to maintain the code in the future.

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

1 Comment

Thanks for reply Brian, Your suggestion is really helpful

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.