Comments (8)
仔细一想,如果已经实现了 _.difference,我们把 _.without 的参数放入数组,然后传入 _.difference 就 ok 了!倒过来就不行了(思考下为什么)。
感觉倒过来也可以
var args = _.union(arg1, arg2, ...);
args.unshift(array);
_.without.apply(null, args);
我哪里错了吗?
from underscore-analysis.
_.without.apply(null, args);
这步的结果还是 args 吧?不知道我有没有理解错 @WangBoxue
from underscore-analysis.
@hanzichi
https://jsfiddle.net/WangBoxue/1z03b4h4/
apply() 方法在指定 this 值和参数(参数以数组或类数组对象的形式存在)的情况下调用某个函数。
所以执行_.without.apply(null, args)
相当于_.without(array, elements in arg1, elements in arg2, ...)
from underscore-analysis.
@WangBoxue 的确如此,感谢指出!
from underscore-analysis.
反过来实现可能需要通知 _.without()
是否是 _.difference()
来调用它。如果是,则展开参数中的所有数组,不然则不展开。当然,前提是 _.difference()
要保证调用 _.without()
时没有传递非数组元素。
_.without = function(array) {
var rest = (this == 'difference') ? _.flatten(arguments, false, false, 1) : Array.prototype.slice.call(arugments, 1);
return _.filter(array, function(value) {
return !_.contains(rest, value);
});
};
_.difference = function(array) {
/** filter out arguments which is not an array firstly */
var args = _.flatten(arguments, true, true, 1).unshift(array);
/**
* let the method `_.without()` know that whether it's called by `_.difference()`,
* if so, then flatten all the arrays from argments,
* if not, then do not flatten.
*/
return _.without.apply('difference', args);
};
from underscore-analysis.
hey I have another question if you dont mind?
So in _.sample function, what is the point of having a Math.max(0,n) passed in _.shuffle function, instead of passing just 'n' ? also why does it check for object before using shuffle? because shuffle function already checks for objects? it that redundant ? please let me know what you think?
from underscore-analysis.
also, why does _.map function use cb(iteratee, context), instead of optimizeCb(iteratee, context) ? you cannot pass anything else but a function in _.map right? so it should be optimizeCb I think, i tried to pass other stuff but it kind of broke the _.map code. Please let me kow what you think? thanks a ton!
from underscore-analysis.
@shaunzeng Math.max 是过滤负数= =
from underscore-analysis.
Related Issues (20)
- JavaScript 数组乱序 HOT 13
- 浅谈 underscore 内部方法 group 的设计原理 HOT 3
- 带注释的源码中var _ = function (obj) {....}中有个疑问 HOT 4
- 关于 bind 你可能需要了解的知识点以及使用场景 HOT 2
- bind 方法的兼容实现 HOT 1
- JavaScript 函数节流和函数去抖应用场景辨析 HOT 17
- underscore 函数去抖的实现 HOT 20
- underscore 函数节流的实现 HOT 8
- 从斐波那契数列求值优化谈 _.memoize 方法 HOT 6
- Function Functions 相关源码拾遗
- 浅谈 Web 中前后端模板引擎的使用 HOT 19
- Underscore _.template 方法使用详解 HOT 3
- Underscore 整体架构浅析 HOT 15
- 源码解读这半年 HOT 20
- 疑问:模块导出中,向后兼容老的require() API HOT 1
- 源码
- 打扰一下,有一个问题想咨询你一下 HOT 9
- 你好请教个问题 HOT 1
- 你好...在谷歌浏览器中运行`Object.prototype.toString().call(abb)`会报错 //var abb = true HOT 2
- 關於underscore源碼中提到了25個扩展方法之疑問? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from underscore-analysis.