Is it possible to reflect the arguments of a Javascript function?

后端 未结 8 1785
孤城傲影
孤城傲影 2020-12-02 23:21

Is it possible to get all of the arguments a Javascript function is written to accept? (I know that all Javascript function arguments are \"optional\")? If

8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-03 00:00

    Now when you say outside the body of the function I can only imagine that you want to know what the names of the parameters are? Because as far as the values go, you already know what arguments you are passing. Other answers have said you can get the length of the function, which is the number of parameters it explicitly declares. Now if you want to know the names outside the function, how about the toString hack?

    Consider

    function f(oh, hi, there) {
        return hi + there / oh;
    }
    

    Then

    alert(f);
    

    What do you see? RIght, just regex them out! Okay, SORRY to bring this up. Perhaps it is not standard ECMAScript, but it, uh, works in Chrome....

提交回复
热议问题