[英]Where can I find the source code for the Javascript `typeof` operator?
I'm looking to see how the typeof
operator in JavaScript knows that an object is a function.我想看看 JavaScript 中的typeof
运算符如何知道 object 是 function。
To be precise, I'm wondering how I can take the function 'body' and reverse engineer it to determine what arguments it expects.确切地说,我想知道如何获取 function 'body' 并对其进行逆向工程以确定它期望的 arguments 。 The arguments
property seemed close, but is only available within the evaluated function. arguments
属性似乎很接近,但仅在评估的 function 中可用。
Neither uneval()
or toSource()
appear to do quite what I want, in addition to being obsolete.除了过时之外, uneval()
或toSource()
似乎都没有做我想要的。
The specification shows that: 规范表明:
Objects that do not implement [[Call]]
are object
s没有实现[[Call]]
的对象是object
Objects that do implement [[Call]]
are function
s实现[[Call]]
的对象是function
s
( [[Call]]
is an "internal property" of objects - it's not exposed directly to interactable running JavaScript) ( [[Call]]
是对象的“内部属性”——它不直接暴露给可交互的运行 JavaScript)
So anything that can be ()
d is a function
.所以任何可以是()
d 的东西都是function
。 This will even include Proxies, since proxies can be called as if they were functions.这甚至包括代理,因为代理可以像函数一样被调用。
For the other question:对于另一个问题:
I'm wondering how I can take the function 'body' and reverse engineer it to determine what arguments it expects我想知道如何获取 function 'body' 并对其进行逆向工程以确定它期望的 arguments
The simplest method would be to turn the function into a string and use a regex to parse the parameter list:最简单的方法是将 function 转换为字符串并使用正则表达式解析参数列表:
function sum(a, b) { return a + b; } const argList = String(sum).match(/\((.*)\)/)[1]; console.log(argList.split(/,\s*/)); // depending on how complicated the list is // you may need to make the above pattern more elaborate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.