V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
yuann72
V2EX  ›  问与答

JS 怎么把函数的参数(未知个数)传递给另一个函数?

  •  
  •   yuann72 · 2016-08-16 11:13:24 +08:00 · 4286 次点击
    这是一个创建于 2812 天前的主题,其中的信息可能已经有所发展或是发生改变。
    function A(/*未知个数的参数*/){
    console.log(/*传递给另一个函数*/);
    }
    A(1,2,3,4);
    19 条回复    2016-08-16 13:27:00 +08:00
    BuilderQiu
        1
    BuilderQiu  
       2016-08-16 11:19:37 +08:00
    了解下: arguments
    66beta
        2
    66beta  
       2016-08-16 11:21:49 +08:00
    function aaa() {return bbb(arguments)}
    function bbb(){console.log(arguments[0])}

    aaa(1,2,3,4)
    shyling
        3
    shyling  
       2016-08-16 11:22:01 +08:00
    function got(...args){console.log(args)}
    got(1,2,3,4)
    tomato3
        4
    tomato3  
       2016-08-16 11:24:58 +08:00
    ```
    function A () {
    console.log(arguments)
    }
    A(123,134,45)

    ```
    morethansean
        5
    morethansean  
       2016-08-16 11:27:47 +08:00   ❤️ 2
    function A() {
    B.apply(this, arguments);
    }

    function B() {
    /** code here */
    }

    A(1,2,3,4,5);
    yuann72
        6
    yuann72  
    OP
       2016-08-16 11:30:43 +08:00
    @tomato3
    @66beta
    这样写 输出的是 arguments 这个对象 // [1, 2, 3 , 4]
    跟直接运行 console.log(1,2,3,4) 输出的不一样 // 1 2 3 4
    yuann72
        7
    yuann72  
    OP
       2016-08-16 11:36:38 +08:00
    @morethansean 没看懂怎么用....
    ETiV
        8
    ETiV  
       2016-08-16 11:39:49 +08:00 via iPhone
    查文档: arguments 、 apply

    扩展: call 、 bind
    Seita
        9
    Seita  
       2016-08-16 11:40:04 +08:00
    你需要了解一下 Function.apply
    plantain
        10
    plantain  
       2016-08-16 11:41:11 +08:00 via Android   ❤️ 1
    function a(...p){b(...p)};
    a(1,2,3)
    airyland
        11
    airyland  
       2016-08-16 11:45:28 +08:00 via iPhone
    楼上正解,但是要 babel 编译
    dosin
        12
    dosin  
       2016-08-16 12:01:25 +08:00 via iPhone
    5 楼说的优雅
    yangg
        13
    yangg  
       2016-08-16 12:03:50 +08:00
    var log = console.log.bind(console);
    66beta
        14
    66beta  
       2016-08-16 12:05:00 +08:00
    @yuann72 你到底要做什么功能呢?
    console.log(1,2,3,4) 是打印四个参数
    console.log([1,2,3,4])
    yuann72
        15
    yuann72  
    OP
       2016-08-16 12:41:26 +08:00
    @66beta 我本来要的功能是把 console.log 改名为 其他
    就是这个 我从其他地方得到的答案 :var log = console.log.bind(console)
    不过我一楼描述错了,不过你们给的回复 我也可以用到其他地方
    mdluo
        16
    mdluo  
       2016-08-16 12:54:52 +08:00
    典型的 X-Y Problem
    finian
        17
    finian  
       2016-08-16 13:01:35 +08:00
    function A() { console.log.apply(console, [].slice.call(arguments)) }
    ljbha007
        18
    ljbha007  
       2016-08-16 13:02:39 +08:00
    5 楼正解
    BOYPT
        19
    BOYPT  
       2016-08-16 13:27:00 +08:00
    ES6 的话有新语法:
    function fun1(...theArgs) {
    console.log(theArgs);
    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5573 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 06:48 · PVG 14:48 · LAX 23:48 · JFK 02:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.