Global impact | jQuery vs. backbone

The backbone system provides itself in the first 50 lines of code.

JQuery in the last 50 lines of code or so.

Why is the difference?

Is this just a random choice of the developer. Or is there a goal that I do not see here.

Github backbone

Backbone = root.Backbone = {}; // line 33 

JQuery Latest

 window.jQuery = window.$ = jQuery; // near bottom..can someone post github link 
+4
source share
3 answers

The backbone apparently uses object prototyping and then adds features / functions.

jQuery, it seems, creates a closure and expands it in the global area later, because it is possible not to set "$", but only "jQuery" in noConflict-Mode. This seems to also ensure that the ready-to-Event does not start too soon before the entire action is determined, since JS may have some difficult race conditions with its callbacks ...

+2
source

Developer's choice based on deployment context.

  • The backbone (and, in particular, Underscore) is written as a general-purpose JavaScript library intended for optionally accessible on the server side (Node.js) or otherwise using the appearance of the browser. The context in which it works inside is more variable, and its attachment to this context is more flexible for use with things like common.js and require.js. Thus, the focus is more on the environment and less on the accessibility of the library itself, especially because Backbone is used in environments where namespaces will usually be more specific and customizable (including rich browser applications in which the developer will be more involved in these issues).

  • jQuery is designed for use in a web browser. Therefore, the context is fixed, and jQuery can build the library in the namespace. The focus for jQuery then switches to easy accessibility and usability in a specific place (for example, $ or jQuery ), avoiding conflicts in a often polluted browser environment: initially providing a life buoy in a mess, and not when installing with a more complex structured environment (as evidenced by how it becomes the custodian of plugins, and does not increase modularity).

+1
source
Good question. The backbone system was created to work on the server side or on the client side. jQuery was created to work in a DOM environment where a global window object must exist. In a server environment like Node, a global object is called global .

You were close, but the actual part in the source of the baseline that sets this is actually the first two lines and the last line:

 (function(){ var root = this; ... }).call(this); 

Like most frameworks, Backbone runs with an anonymous function to encapsulate its configuration, but in this case it calls this method in the context of the global scope this , which allows window in the web client and global on a server like node.

Note that jQuery probably doesn't do anything like this, because it relies on dozens of DOM methods like document.getElementById . However, you can create a virtual DOM environment on the server to load jQuery. See Projects of the JSDOM Type .

Here are the conversation slides that I gave, where I cover this exact topic: https://speakerdeck.com/krunkosaurus/deep-dive-into-backbone-dot-js-internals-plus-underscore-dot-js

0
source

Source: https://habr.com/ru/post/1436705/


All Articles