underscore.js source code reading (2)

  Today saw the next underscore the restArgs function and function createAssigner

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

//(http://ariya.ofilabs.com/2013/03/es6-and-rest-parameter.html)
// This accumulates The arguments passed INTO AN Array, After A GIVEN index.
// The rest and ES6 similar characteristics, according to the index position of a given parameter, the parameter will be filled to a position after the index
// (see https://github.com/jashkenas/underscore/issues/2542) var restArgs = function ( FUNC, startIndex ) { == = startIndex startIndex null func.length -? 1 : + startIndex; return function (


) {
var length = Math.max(arguments.length - startIndex, 0),
rest = Array(length),
index = 0;
for (; index < length; index++) {
rest[index] = arguments[index + startIndex];
}
switch (startIndex) {
case 0: return func.call(this, rest);
case 1: return func.call(this, arguments[0], rest);
case 2: return func.call(this, arguments[0], arguments[1], rest);
}
var args = Array(startIndex + 1);
for (index = 0; index < startIndex; index++) {
args[index] = arguments[index];
}
args[startIndex] = rest;
return func.apply(this, args);
};
};

  createAssignerFunction is mainly used in the following three areas:

1
2
3
4
5
6
7
8
9
 // Extend a given object with all the properties in passed-in object(s).
_.extend = createAssigner(_.allKeys);

// Assigns a given object with all the own properties in the passed-in object(s).
// (https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/assign)
_.extendOwn = _.assign = createAssigner(_.keys);

// Fill in a given object with default properties.
_.defaults = createAssigner(_.allKeys, true);

   _.extendFunction expansion function is used to dry the object attributes; the _.extendOwnfunction of the object will only have its own attributes; _.defaultsthe function is, if the same key, the latter will not overwrite the previous, a key to take the first value appears once as a key -value pairs.
  In addition, a large column  underscore.js reading source (2) three methods can accept> = a parameter to .extend an example, .extend (A, b, c) will be b, c two to cover the key object to a.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// An internal function for creating assigner functions . 
Internal function for realizing the dispenser functions // var createAssigner = function ( keysFunc, Defaults ) { // return function // classic closure (Defaults parameter is referenced in the function returns ) // returns the number of function parameters of> = 1 // start the second key parameter of the object to "inherit" the first parameter to return function ( obj ) { var length = arguments .length; // only passing one parameter (or 0?) // or the first argument is null IF (Defaults) obj = Object (obj); IF (length < 2 || obj == null ) return obj;











// Enumeration except the first parameter object parameters // i.e. arguments [. 1], arguments [2] ... for ( var index = . 1 ; index <length; index ++) { var Source = arguments [index], / / extraction target value of the parameter keys // keysFunc parameter indicates _.keys // or _.allKeys keys = keysFunc (Source), L = keys.length; // key to traverse the object for ( var I = 0 ; I <L; I ++) { var Key = Keys [I]; // _.extend methods and _.extendOwn ! // defaults parameter is not passed, i.e., as defaults to true // i.e. certainly performed obj [key] = source [key] key // directly behind the object obj cover















========================================== // // _.defaults method , Defaults parameter to true // i.e.! to Defaults to false // if and only if obj [key] undefined when coverage // i.e., if the same key value, whichever value the value of the earliest // _.defaults in the same key is taken as the first appearance of IF (Defaults || obj [key] ===! void 0 obj [key] = Source [key]); } } // returns have inherited property behind a first object parameters a parameter object return obj; }; };












Guess you like

Origin www.cnblogs.com/dajunjun/p/11711028.html