Underscore.js Analysis

Source: Internet
Author: User

Underscore.js source and suitable for the first time to see the source, because the file is relatively small, and no reliance, read more relaxed. The code is also very concise to write.

I'm looking at 1.7 of the source code, and here are some of the more interesting places that I think

1.

_.isundefined = function (obj) {    returnvoid0;};

There are several places in the code that use void 0 instead of undefined to determine if an object is undefined. This is said to be because some browsers allow changing the value of undefined. such as undefined = 1

2.

Used to use jquery used to Noconflict, has been curious how to achieve. It turns out that Underscore.js's implementation is so simple.

var  This ; var previousunderscore == function () {    = previousunderscore;     return  This ;};

3.

Underscore.js supports two types of invocation, one that is called directly with _.func (params), and that is called in an oop-like manner.

function (element, index) {    console.log (element);}); _ ([' A ', ' B ', ' C ']). Each (function(element, index) {    console.log (element);});

How did this happen?

First, the following functions are defined, such as

_.each = _.foreach =function(obj, iteratee, context) {if(obj = =NULL)returnobj; Iteratee=Createcallback (iteratee, context); varI, length =obj.length; if(length = = = +length) {       for(i = 0; i < length; i++) {iteratee (obj[i], I, obj); }    } Else {      varKeys =_.keys (obj);  for(i = 0, length = keys.length; i < length; i++) {iteratee (Obj[keys[i]], keys[i], obj); }    }    returnobj; };

If you think of objects in JavaScript as classes, the definition is similar to defining the static member function of class ' _ ', which can be called directly through _.func. However, the instance of class ' _ ' is not callable because it is a new _ ().

Just OOP relies on the following two functions

 var_ =function(obj) {if(objinstanceof_)returnobj; if(! ( This instanceof_))return New_ (obj);  This. _wrapped =obj;  }; //ADD your own custom functions to the underscore object._.mixin =function(obj) {_.each (_.functions (obj),function(name) {varFunc = _[name] =Obj[name]; _.prototype[name]=function() {        varargs = [ This. _wrapped];        Push.apply (args, arguments); returnResult.call ( This, Func.apply (_, args));    };  });  }; //Add all of the underscore functions to the wrapper object._.mixin (_);

First, the first function executes when it calls _ ([' A ', ' B ', ' C '). What it does is to turn an obj into an instance of ' _ ' (New _ ()), and to save obj to the _wrapped of the instance.

The implementation of this function is also interesting. When calling _[' a ', ' B ', ' C ', the first time to execute to the second sentence, this is the global window, so if (! ( This instanceof _) is established, so execute new _ (obj), this time the second time into the function, this is the new _ instance instance, so directly to the third row, that is, the this_wrapped assignment to obj.

Tbc...

Underscore.js Analysis

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.