
私たちは、javascriptの基礎に関する本を読んで、得られた知識をより良くしたい5番目の「B」Vasya Pupkinの学生の観点からこのソースを準備しています。
初期カット
- モジュール作成または「ユニバーサル調達」
コード(function() { // (window exports) var root = this; // underscore, . var previousUnderscore = root._; // if (typeof exports !== 'undefined') { if (typeof module !== 'undefined' && module.exports) { exports = module.exports = _; } exports._ = _; } else { root._ = _; } // _.noConflict = function() { root._ = previousUnderscore; return this; }; // , if (typeof define === 'function' && define.amd) { define('underscore', [], function() { return _; }); } }.call(this)); //
- initial _(その_.function(何か)または_(何か)のように使用できるもの。function())
コード// - - . var _ = function(obj) { if (obj instanceof _) return obj; if (!(this instanceof _)) return new _(obj); this._wrapped = obj; };
- 短いリンクを作成する
コードvar ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.prototype; // var push = ArrayProto.push, slice = ArrayProto.slice, concat = ArrayProto.concat, toString = ObjProto.toString, hasOwnProperty = ObjProto.hasOwnProperty; // var nativeIsArray = Array.isArray, nativeKeys = Object.keys, nativeBind = FuncProto.bind;
- バージョンを追加
コード_.VERSION = '1.7.0';
- 各要素で使用される関数を作成し、要素を反復する他の関数内で
コード// , // this func context var createCallback = function(func, context, argCount) { if (context === void 0) return func; // - switch (argCount == null ? 3 : argCount) { // func case 1: return function(value) { // , -, func return func.call(context, value); // }; case 2: return function(value, other) { return func.call(context, value, other); }; case 3: return function(value, index, collection) { return func.call(context, value, index, collection); }; case 4: return function(accumulator, value, index, collection) { return func.call(context, accumulator, value, index, collection); }; } return function() { return func.apply(context, arguments); // , func , }; // c , arguments }; // , _.iteratee = function(value, context, argCount) { if (value == null) return _.identity; // if (_.isFunction(value)) return createCallback(value, context, argCount); if (_.isObject(value)) return _.matches(value); // return _.property(value); // };
- この_.function(処理するオブジェクト、より多くの引数)
コードコレクション用_.each = _.forEach = function(obj, iteratee, context) { }; _.map = _.collect = function(obj, iteratee, context) { }; _.reduce = _.foldl = _.inject = function(obj, iteratee, memo, context) { }; _.reduceRight = _.foldr = function(obj, iteratee, memo, context) { }; _.find = _.detect = function(obj, predicate, context) { }; _.filter = _.select = function(obj, predicate, context) { }; _.reject = function(obj, predicate, context) { }; _.every = _.all = function(obj, predicate, context) { }; _.some = _.any = function(obj, predicate, context) { }; _.contains = _.include = function(obj, target) { }; _.invoke = function(obj, method) { }; _.pluck = function(obj, key) { }; _.where = function(obj, attrs) { }; _.findWhere = function(obj, attrs) { }; _.max = function(obj, iteratee, context) { }; _.min = function(obj, iteratee, context) { }; _.shuffle = function(obj) { }; _.sample = function(obj, n, guard) { }; _.sortBy = function(obj, iteratee, context) { }; // , , _.groupBy = group(function(result, value, key) { }); _.indexBy = group(function(result, value, key) { }); _.countBy = group(function(result, value, key) { }); _.sortedIndex = function(array, obj, iteratee, context) { }; _.toArray = function(obj) { }; _.size = function(obj) { }; _.partition = function(obj, predicate, context) { };
配列用// , , , _.first = _.head = _.take = function(array, n, guard) { }; _.initial = function(array, n, guard) { }; _.last = function(array, n, guard) { }; _.rest = _.tail = _.drop = function(array, n, guard) { }; _.compact = function(array) { }; _.flatten = function(array, shallow) { }; _.without = function(array) { }; _.uniq = _.unique = function(array, isSorted, iteratee, context) { }; _.union = function() { }; _.intersection = function(array) { }; _.difference = function(array) { }; _.zip = function(array) { }; _.object = function(list, values) { }; _.indexOf = function(array, item, isSorted) { }; _.lastIndexOf = function(array, item, from) { }; _.range = function(start, stop, step) { };
機能用// _.bind = function(func, context) { }; _.partial = function(func) { }; _.bindAll = function(obj) { }; _.memoize = function(func, hasher) { }; _.delay = function(func, wait) { }; _.defer = function(func) { }; _.throttle = function(func, wait, options) { }; _.debounce = function(func, wait, immediate) { }; _.wrap = function(func, wrapper) { }; _.negate = function(predicate) { }; _.compose = function() { }; _.after = function(times, func) { }; _.before = function(times, func) { }; _.once = _.partial(_.before, 2); // before, 2
オブジェクト用_.keys = function(obj) { }; _.values = function(obj) { }; _.pairs = function(obj) { }; _.invert = function(obj) {}; _.functions = _.methods = function(obj) {}; _.extend = function(obj) { }; _.pick = function(obj, iteratee, context) { }; _.omit = function(obj, iteratee, context) { }; _.defaults = function(obj) { }; _.clone = function(obj) { }; _.tap = function(obj, interceptor) { }; _.isEqual = function(a, b) { }; _.isEmpty = function(obj) { }; _.isElement = function(obj) { }; _.isArray = nativeIsArray || function(obj) { }; // - _.isObject = function(obj) { }; _.each(['Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp'], function(name) { }); // if (!_.isArguments(arguments)) { _.isArguments = function(obj) { }; // , } if (typeof /./ !== 'function') { _.isFunction = function(obj) { }; } _.isFinite = function(obj) {}; _.isNaN = function(obj) {}; _.isBoolean = function(obj) {}; _.isNull = function(obj) {}; _.isUndefined = function(obj) {}; _.has = function(obj, key) {};
ユーティリティ_.identity = function(value) {}; _.constant = function(value) {}; _.noop = function(){}; _.property = function(key) { }; _.matches = function(attrs) { }; _.times = function(n, iteratee, context) { }; _.random = function(min, max) { }; _.now = Date.now || function() {}; _.escape = createEscaper(escapeMap); _.unescape = createEscaper(unescapeMap); _.result = function(object, property) { }; _.template = function(text, settings, oldSettings) { }; _.chain = function(obj) { };
- あなたがそうすることができるように関数を処理する_(処理するオブジェクト)。
コード// : _(a).f1().f2().f3() var result = function(obj) { return this._chain ? _(obj).chain() : obj; }; // , _.mixin = function(obj) { _.each(_.functions(obj), function(name) { var func = _[name] = obj[name]; _.prototype[name] = function() { // c _ _() var args = [this._wrapped]; // _, "" push.apply(args, arguments); return result.call(this, func.apply(_, args)); // , , _(); }; }); }; // , _.mixin(_); // , , _.each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) { var method = ArrayProto[name]; _.prototype[name] = function() { var obj = this._wrapped; method.apply(obj, arguments); // this- . if ((name === 'shift' || name === 'splice') && obj.length === 0) delete obj[0]; // . , return result.call(this, obj); }; }); // _.each(['concat', 'join', 'slice'], function(name) { var method = ArrayProto[name]; // _.prototype[name] = function() { return result.call(this, method.apply(this._wrapped, arguments)); }; }); // x _().f1().f2().f3() _.prototype.value = function() { return this._wrapped; };
JSカット
- 変数処理
コードvar root = this // , this var i, length = obj.length; // , for (i = 0, length = keys.length; i < length; i++) { iteratee(obj[keys[i]], keys[i], obj); } for (; i < length; i++) if (array[i] === item) return i; // i slice.call(array, Math.max(array.length - n, 0)); // args.concat(slice.call(arguments)) // for (var key in obj) if (_.has(obj, key)) keys.push(key); // obj[prop] = source[prop]; //
- 関数処理
コード(function() { }.call(this)); // , this if (!(this instanceof _)) return new _(obj); // , - var createCallback = function(func, context, argCount) { return function(value) { // , return func.call(context, value); }; }; _.each = _.forEach = function(obj, iteratee, context) {}; // _ _.find = _.detect = function(obj, predicate, context) { predicate = _.iteratee(predicate, context); // predicate _.some(); // , , }; _.some(obj, function(value, index, list) { }); // // , var group = function(behavior) { // return function(obj, iteratee, context) { // , behavior(result, value, key); // - return result; }; }; _.indexBy = group(function(result, value, key) { // result[key] = value; }); // , , , , , , var flatten = function(input, shallow, strict, output) { for (var i = 0, length = input.length; i < length; i++) { flatten(value, shallow, strict, output); // } }; // _.flatten = function(array, shallow) { return flatten(array, shallow, false, []); }; // var x = func.bind(obj), , , : x(), // - obj.func(), this func obj nativeBind.apply(func, slice.call(arguments, 1)) // , // this obj[key] = _.bind(obj[key], obj); // _.memoize = function(func, hasher) { var memoize = function(key) {}; memoize.cache = {}; // return memoize; }; // setTimeout(function(){ return func.apply(null, args); }, wait); // , _.property = function(key) { return function(obj) { return obj[key]; // }; }; // _.prototype[name] = function() { // name var args = [this._wrapped]; push.apply(args, arguments); return result.call(this, func.apply(_, args)); }; // : var x = new _; x[name]();
- チェック
コードtypeof exports !== 'undefined' // if (length === +length) // var keys = obj.length !== +obj.length && _.keys(obj), // , _.keys length = (keys || obj).length, // , , results = Array(length), currentKey; memo = obj[keys ? keys[index++] : index++]; // , , if (!index) throw new TypeError(reduceError); // - if (array == null) return void 0; // if (n == null || guard) return array[0]; if (n < 0) return []; return slice.call(array, 0, n); if (!(this instanceof bound)) // this new bound _.isBoolean = function(obj) { // return obj === true || obj === false || toString.call(obj) === '[object Boolean]'; }; // , func if (nativeBind && func.bind === nativeBind) return nativeBind.apply(func, slice.call(arguments, 1)); hasOwnProperty.call(source, prop) // ( ) if (key in obj) result[key] = obj[key]; // if (obj[prop] === void 0) obj[prop] = source[prop]; // // try { var render = new Function(settings.variable || 'obj', '_', source); } catch (e) { // e.source = source; throw e; // }
- 美しい瞬間
コードwhile (index--) { // currentKey = keys ? keys[index] : index; memo = iteratee(memo, obj[currentKey], currentKey, obj); } // - value, this - value, args // func.apply(x, [1, 2, 3, 4]) x.func(1, 2, 3, 4), func.call(x, [1, 2, 3, 4]) - x.func([1, 2, 3, 4]) (isFunc ? method : value[method]).apply(value, args) var mid = low + high >>> 1; // 2 // _ , _.each(['Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp'], function(name) { _['is' + name] = function(obj) { return toString.call(obj) === '[object ' + name + ']'; }; });
- 引数セット
コード// - (iteratee) obj, this- - context // obj - function(obj, iteratee, context) {}; // , - - , // - , value - , index - function(accumulator, value, index, collection) {}; memo = iteratee(memo, obj[currentKey], currentKey, obj); // predicate , true, function(obj, predicate, context) {}; // , / , /; function(value, index, list) { } // method , -, function(obj, method) {}; // key - , , . function(obj, key) {}; // attrs - key, function(obj, attrs) { }; // guard , function(obj, n, guard) {}; // function(array, n, guard) {}; // , function(func){}; // - function(obj){};
残りを見て、Firebugとドキュメントの使用方法を学び、すべてを元に戻しますが、Vasyaは宿題のままです。
PS小さなホリバルキ