推荐关注
› Meteor
› JSLint - a JavaScript code quality tool
› jsFiddle
› D3.js
› WebStorm
推荐书目
› JavaScript 权威指南第 5 版
› Closure: The Definitive Guide
Stefango

.map(window.open) 和 .map((one)=>window.open(one)) 的区别是啥?

  •  
  •   Stefango · Apr 6, 2022 · 4012 views
    This topic created in 1671 days ago, the information mentioned may be changed or developed.

    这两行代码的执行结果咋不一样(一个打开新窗口,一个在当前窗口打开新标签页)? this 不同?

    `https://baidu.com
    https://163.com`.split(/\r|\n/ig).map(window.open)
    
    
    `https://baidu.com
    https://163.com`.split(/\r|\n/ig).map((one)=>window.open(one))
    
    16 replies  •  2022-04-08 09:50:24 +08:00
    wiluxy
    wiluxy
    Apr 6, 2022❤️4
    1
    map 的回调函数会传入三个值 item,index,arr ,window.open 的参数也不止一个,第一种写法相当于是把他们三个值都传到了 window.open 里面,第二种是显式指定了只传第一个参数
    Stefango
    Stefango
    OP
    Apr 6, 2022❤️1
    2
    @wiluxy 对的!
    SorcererXW
    SorcererXW
    Apr 6, 2022
    3
    后者更保险
    noe132
    noe132
    Apr 6, 2022
    4
    interface Array<T> {
    map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[];
    }
    interface Window {
    open(url?: string | URL, target?: string, features?: string): WindowProxy | null;
    }

    这种问题还有另外一个常见的地方就是 map to parseInt
    ["20", "20", "20", "20", "20", "20", "20"].map(parseInt)
    // [20, NaN, NaN, 6, 8, 10, 12]
    FP 中完美的 Functor.map 只需要一个参数 v 就可以了,所以很多 fp 的轮子会专门有只有一个参数版本的 map 。
    noe132
    noe132
    Apr 6, 2022
    5
    还有一个概念在 FP 中很常见叫做 Partial Application ,其实就是让多参数函数固定住几个参数。但通常是从左边开始。像这里这种我们也可以称作 Partial Application ,只不过没那么 FP 了。

    const open = v => window.open(v)
    arr.map(open)
    就类似于把后 2 个参数固定成了 undefined
    sudoy
    sudoy
    Apr 6, 2022 via iPhone
    6
    一楼正解
    YuTengjing
    YuTengjing
    Apr 6, 2022
    7
    第二种方式完全是多余操作
    autoxbc
    autoxbc
    Apr 6, 2022
    8
    个人踩坑经验,除非 map 里的函数是你自己创建的,不然永远不要用第一种写法
    mxT52CRuqR6o5
    mxT52CRuqR6o5
    Apr 6, 2022
    10
    @YuTengjing 1#说的那么清楚还多余,执行结果都不符合预期了
    YuTengjing
    YuTengjing
    Apr 6, 2022
    11
    @mxT52CRuqR6o5 #10 你真的看懂了吗?还结果不符合预期,你拷到 chrome 里面跑一下都不会吗?
    YuTengjing
    YuTengjing
    Apr 6, 2022
    12
    @mxT52CRuqR6o5 #10 Sorry, 逻辑确实不一样,不过我这边跑了一下,效果都是直接打开一个百度的窗口
    shintendo
    shintendo
    Apr 7, 2022
    13
    常见的两个坑:
    1 .map(parseInt)
    parseInt 有可选的第二个参数(进制)
    2 Vue 写事件绑定 @click="getList"
    getList 如果有可选参数(页码),这个参数会接收到$event
    iqoo
    iqoo
    Apr 7, 2022
    14
    用 typescript 第一种写法根本就过不了编译
    ragnaroks
    ragnaroks
    Apr 7, 2022
    15
    我想到 dotnet framework 的大坑了,以前因为这个导致过几个订单数据对不上的生产事故

    method (Enumerable<T> list)

    method (T[] list)

    method (T v1,T v2)
    LeegoYih
    LeegoYih
    Apr 8, 2022
    16
    map 的参数是方法的引用,简单的说就是 map 需要传入一个方法。
    map((one)=>window.open(one)),实际上是传了个匿名函数,匿名函数也称 lambda 表达式。

    第一种方法等价于 map((a,b,c)=>window.open(a,b,c)),TS 编译不通过因为 map 需要函数的出入参类型与 window.open 提供的不匹配,实际上静态语言都如此。JS 中这 2 种都能用是因为它不校验出入参类型和参数数量。

    PS:这里应该用 forEach ,map 不太合适
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5048 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 53ms · UTC 08:33 · PVG 16:33 · LAX 00:33 · JFK 03:33
    ♥ Do have faith in what you're doing.