祁梦

文章 评论 浏览 30

祁梦 2022-05-04 13:56:37
function intersection(a,b){
        const res= []
        const [min,max] = a.length > b.length ? [b,a] : [a,b]
        // 那个参数短 那么设置这个长度为min 减少循环次数
        while (min.length){ // min还有长度
            const item = min.shift() // 弹出第一个值
            const index = max.indexOf(item) // 查询弹出的值在另一个数组的索引
            if(index !== -1){ // 如果查询到下标
                max.splice(index,1) // 在另一个数组中删除查询到的这一项
                res.push(item) // 推送到返回值中
            }
        }
        return res
    }

第 59 题:给定两个数组,写一个方法来计算它们的交集

祁梦 2022-05-04 13:41:46

17.使用 ~i 代替 i > -1

我们有时需要判断数组、字符串存在某个元素时,进行某项操作:

if(arr.indexOf(item) > -1) {
    // some code
}

这时候可以使用否操作 ~ 来装X

if(~arr.indexOf(item)) {
    // some code
}

18.柯里化 bindapplycall

出自 JavaScript 框架设计,非常骚气的黑魔法

const bind = function(bind) {
    return {
        bind: bind.bind(bind),
        call: bind.bind(bind.call),
        apply: bind.bind(bind.apply)
    }
}(Function.prototype.bind)

使用

const concat = bind.apply([].concat);
concat([1,2,3], [4,5]);   //    [1, 2, 3, 4, 5]
//等价于
[].concat.apply([1,2,3], [4, 5]);

const concat1 = bind.call([].concat);
concat1([1,2,3], 4, 5, 6)   // [1, 2, 3, 4, 5, 6]
//等价于
[].concat.call([1,2,3], 4, 5, 6);

const concat2 = bind.bind([].concat);
concat2.bind([1,2,3], 14, 15)();

//等价于
[].concat.bind([1,2,3])(14, 15);

用一张简单的代码图表示其中的原理:

柯里化之后给了我们多一次传参的机会,这样就可通过判断返回不同的结果了。

19.最简单的柯里化函数

1.支持每次只传一个参数的 正宗柯里化函数

function curry(fn) {
    function inner(len, args) {
        if(len <= 0) return fn.apply(null, args);

        return function(x) {
            return inner(len - 1, args.concat(x))
        }
    }

    return inner(fn.length, []);
}

使用:

function sum(x, y, z){
    return x + y + z;
}

curry(sum)(12)(13)(14)     // 39

2.支持一次传多个参数的 简化柯里化函数

function curry(fn) {

    function inner(len, args) {
        if(len <=0) return fn.apply(null, args);

        return function() {
            return inner(len - arguments.length, args.concat([].slice.apply(arguments)))
        }
    }
    return inner(fn.length, []);
}

使用

curry(sum)(12, 13)()(14)    // 39

20.最简单的partial技术实现

与柯里化相似,柯里化缺点在于每次参数都是通过push的,但有些时候有些参数是已经拥有的,我们需要做的是填充没有的参数,这个时候我们可以使用占位操作来代表需要填充的参数,如undefined,不过推荐使用 Object.create(null) ,因为纯空对象没有原型,因此没有toString、valueOf等一系列继承自Object的方法。

这种技术和柯里化技术很适合在延迟调用的场景中使用。

var _ = Object.create(null);

function partial(fn) {
    var args = Array.prototype.slice.call(arguments, 1);

    return args.length < 1 ? fn : function() {
        var innerArgs = [].slice.call(arguments);

        args.forEach((e,i) => {
            if(e === _) args[i] = innerArgs.shift();
        });
        return fn.apply(null, args)
    }
}

使用

function sum() {
    return Array.prototype.slice.call(arguments).reduce((total, e)=> total + e, 0)
}


const c = partial(sum, 1, 2, _, 4, _);

c(3, 5)   // 15

JS 中那些骚气的小技巧 OR 代码片段

更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文