Thursday 26 September 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...