简体   繁体   中英

how does the for each method in underscore.js work?

In underscore.js the for each method starts with the following:

var each = _.each = _.forEach = function(obj, iterator, context) {

How is it able to do something = something = something? I didn't think you could do that?

See: http://documentcloud.github.com/underscore/docs/underscore.html#section-12

Thanks

That's basically just defining aliases for the function(obj, iterator, context) that is created.

It's the same as doing var a = b = c = d = 0; All those vars are created and have the value 0.

Though the example I gave will actually create separate objects because a number is an intrinsic type. The one in your question will create those objects that all reference the same function.

It's mean that there is creating one local function each for local functions, and creating _.each and _.forEach methods in 'Underscore' scope. Thats all. It`s like

var a , b, c;
a = b = c = 3;
console.log(a,b,c) // 3,3,3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM