In-depth analysis of the Backbone. js framework dependency library Underscore. js role _ basic knowledge

Source: Internet
Author: User
This article mainly introduces the in-depth analysis of Backbone. js framework dependency library Underscore. the role of js, using Node. js friends are familiar with Underscore. :) If you need them, refer to backbone which must depend on underscore. javascript can only be used. It must use functions in underscore to perform basic operations on page elements and processing elements.
Note: backbone can work well with other js libraries, so it is a library rather than a framework.
Underscore does not extend the native object, but calls the _ () method for encapsulation. Once the encapsulation is complete, the js object becomes the Underscore object. You can also use the Value () of the Underscore object () method to obtain the data in the native js object. (Jquery uses the $ () method to obtain the Jquery object)
Underscore has more than 60 functions. According to different processing objects, it can be divided into five categories: Collection class, array class, function class, object class, and tool function class.

Underscore template () function description:

This function contains three templates:

(1) <%>: contains the logic code, which is not displayed after rendering.
(2) <% = %>: data type. Data is displayed after rendering.
(3) <%-%>: converts HTML tags into common strings to avoid code attacks.

Call format:

_.template(templateString, [data], [setting])

Two-way data binding is not implemented.

1. Underscore object Encapsulation
Underscore is not extended in the native JavaScript Object prototype. Instead, like jQuery, it encapsulates data in a custom object ("Underscore object ").
You can call the value () method of an Underscore object to obtain native JavaScript data, for example:

// Define a JavaScript built-in object var jsData = {name: 'data'} // pass _() method: Create an Underscore object. // the prototype of the underscoreData object contains all the methods defined in Underscore. You can use var underscoreData = _ (jsData) at will ); // obtain the native data through the value method, that is, jsData underscoreData. value ();

2. The JavaScript 1.6 built-in method is preferentially called.
Many methods in Underscore have been incorporated into JavaScript1.6. Therefore, the built-in methods provided by the host environment are preferentially called within the Underscore object (if the host environment has implemented these methods ), to improve the function execution efficiency.
For host environments that do not support JavaScript 1.6, Underscore is implemented in its own way, which is completely transparent to developers.
The host environment mentioned here may be the Node. js runtime environment or the client browser.

3. Change the namespace
By default, Underscore uses _ (underline) to access and create objects. However, this name may not comply with our naming rules or cause naming conflicts.
We can use the noConflict () method to change the name of Underscore and restore the value before the _ (underline) variable, for example:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.