我在哪里可以找到 Javascript `typeof` 运算符的源代码?
Where can I find the source code for the Javascript `typeof` operator?
我正在查看 JavaScript 中的 typeof
运算符如何知道一个对象是一个函数。
准确地说,我想知道如何获取函数 'body' 并对它进行逆向工程以确定它需要什么参数。 arguments
属性 看起来很接近,但仅在评估函数中可用。
除了过时之外,uneval()
或 toSource()
似乎都无法满足我的要求。
未实现 [[Call]]
的对象是 object
s
实现 [[Call]]
的对象是 function
s
([[Call]]
是对象的“内部 属性” - 它不直接暴露给可交互的 运行 JavaScript)
所以任何可以 ()
d 的东西都是 function
。这甚至包括代理,因为代理可以像函数一样被调用。
另一个问题:
I'm wondering how I can take the function 'body' and reverse engineer it to determine what arguments it expects
最简单的方法是将函数转为字符串,然后使用正则表达式解析参数列表:
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
我正在查看 JavaScript 中的 typeof
运算符如何知道一个对象是一个函数。
准确地说,我想知道如何获取函数 'body' 并对它进行逆向工程以确定它需要什么参数。 arguments
属性 看起来很接近,但仅在评估函数中可用。
除了过时之外,uneval()
或 toSource()
似乎都无法满足我的要求。
未实现 [[Call]]
的对象是 object
s
实现 [[Call]]
的对象是 function
s
([[Call]]
是对象的“内部 属性” - 它不直接暴露给可交互的 运行 JavaScript)
所以任何可以 ()
d 的东西都是 function
。这甚至包括代理,因为代理可以像函数一样被调用。
另一个问题:
I'm wondering how I can take the function 'body' and reverse engineer it to determine what arguments it expects
最简单的方法是将函数转为字符串,然后使用正则表达式解析参数列表:
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