Thursday, September 26, 2019

"Include" one javascript file to another one




I have 2 separate javascript files



#1.js
String.prototype.format = ....

String.prototype.capitalize = ....

#2.js

//................
var text = "some text{0}".format(var1)
//................


How do I make string#format and string#capitalize available in the second file?



Answer



JavaScript executes globally. Adding both scripts on the page makes them available to each other as if they were in one file.







However, you should note that JavaScript is parsed "linearly" and thus, "first parsed, first served". If the first script needs something in the second script, but the second script hasn't been parsed yet, it will result in an error.



If that happens, you should rethink your script structure.



No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...