Home > Web Front-end > JS Tutorial > body text

JavaScript uses apply and arguments reuse methods_javascript skills

WBOY
Release: 2016-05-16 17:12:30
Original
1071 people have browsed it

First of all, there is a singleton object with many static tool methods attached to it. One of them is each, which is used to traverse arrays or objects.

Copy code The code is as follows:

var nativeForEach = [].forEach
var nativeMap = [].map
var util = {
each: function (obj, iterator, context) {
if (obj == null) return
if (nativeForEach && obj.forEach === nativeForEach) {
                                                                                                                                                                                 ) {
                                                                                                                                                                                              k in obj) {
              if (iterator.call(obj[k] || context, obj[k], k, obj) === true) return
                                                                                                                         
map: function(obj, iterator, context) {
var results = []
if (obj == null) return results
if (nativeMap && obj.map === nativeMap) return obj.map(iterator, context)                                                                                                                                                                                                                                                    . )
       return results
    }
}




There are also utility functions such as every, some, etc. that operate on collections (Array, Hash). Use the util.xx method when using it.

If a collection class is defined, there is collection data inside this class.


Copy code

The code is as follows:

function Collection(data) {

this.data = data || [] // some other property // this.xxx = yyy } Collection.prototype = { // some method }



You can easily copy the methods on util to the collection class, such as




Copy code

The code is as follows:

function copyMethod(clazz, obj) {

for (var method in obj) { clazz.prototype[method] = function() { var args = [].slice.call(arguments) var target = this.data args. unshift(target) obj[method].apply(obj, args) }
}
}
copyMethod(Collection, util)




After copying in this way, the Collection instance will have the method on util, and the collection object (first parameter) operated by util is this.data of Collection. You can directly traverse this.data as follows.




Copy code

The code is as follows:

var coll = new Collection([10, 20, 30 ])

var arr = coll.map(function(k) {
return k - 5
})
console.log(arr) // 5, 15, 25


This mode is used in many open source libraries, such as jQuery, whose $.each/$.map is conveniently copied to $().each/$().map.

Another example is Backbone, whose _.each/_.map/_.every/_.chain (and many more) are copied to the Collection prototype.

Copy code The code is as follows:

// Underscore methods that we want to implement on the Collection .
// 90% of the core usefulness of Backbone Collections is actually implemented
// right here:
var methods = ['forEach', 'each', 'map', 'collect', ' reduce', 'foldl',
'inject', 'reduceRight', 'foldr', 'find', 'detect', 'filter', 'select',
'reject', 'every', ' all', 'some', 'any', 'include', 'contains', 'invoke',
'max', 'min', 'toArray', 'size', 'first', 'head', 'take', 'initial', 'rest',
'tail', 'drop', 'last', 'without', 'difference', 'indexOf', 'shuffle',
'lastIndexOf', 'isEmpty', 'chain'];

// Mix in each Underscore method as a proxy to `Collection#models`.
_.each(methods, function(method) {
Collection .prototype[method] = function() {
var args = slice.call(arguments);
args.unshift(this.models);
return _[method].apply(_, args) ;
};
});

Also, practical methods for object operations such as _.keys / _.values ​​/ _.pairs / _.invert / _.pick have been copied to Backbone.Model (new in 1.0)

Copy code The code is as follows:

var modelMethods = ['keys', 'values', 'pairs', 'invert', 'pick', 'omit'];

// Mix in each Underscore method as a proxy to `Model#attributes`.
_.each(modelMethods, function (method) {
Model.prototype[method] = function() {
var args = slice.call(arguments);
args.unshift(this.attributes);
return _[method ].apply(_, args);
};
});

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template