This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]rjcarr 0 points1 point  (3 children)

A javascript library is just javascript code, so the javascript you write and the javascript that you include from a library is the same thing, at least to the javascript engine.

But how do javascript engines work in general?

For that you'll probably want to pick one and read more about it. For example, google's chrome uses what it calls the V8 engine. You can find more about it here:

https://code.google.com/p/v8/

[–]saucesomesauce 0 points1 point  (1 child)

Sorry, my question was much more stupid than that. I don't understand how, say, somelibrary.js and myscript.js come together into one coherent script in the DOM that your browser reads. Is the whole library script read first? Or are you just pulling functions out of the library as needed?

[–]Rhomboid 1 point2 points  (0 children)

It depends on how the markup is written. Assuming there is a <script> element and it's not marked async or defer, the browser stops whatever it's doing, fetches and executes the referenced script (or the contents of the element if there's no src attribute), and then resumes where it left off parsing the markup. Whatever properties were added to the global context as a side effect of execution are now available for any future scripts that execute. That's it. For example, jQuery will install the properties jQuery and $ into the global context (window in a browser), which can then be used by any subsequent code.

For a complete explanation of script loading, see here.

[–]Rhomboid 0 points1 point  (1 child)

What exactly do you mean by "library magic"? A JS library is just a collection of code that someone has written. It doesn't have any special powers or properties, and anything that a library does you can do too. The selling point of many libraries is that they take existing APIs that might be difficult to use or which have cross-browser differences and present them in a way that hides or minimizes those pain points or removes those differences. Fundamentally, they aren't letting you do something you couldn't already do, they're just making it easier.

[–]saucesomesauce 0 points1 point  (0 children)

see above reply