arrow function和regular function之间没有明显区别。
({}).toString.call(function () {})
"[object Function]"
({}).toString.call(() => {})
"[object Function]"
或
console.dir( (function () {}) )
function anonymous()
arguments: null
caller: null
length: 0
name: ""
prototype: Object
__proto__: ()
<function scope>
console.dir( (() => {}) )
function anonymous()
arguments: (...)
caller: (...)
length: 0
name: ""
__proto__: ()
<function scope>
两者的行为是不同的,并且有一个有效的用例可以告诉两者分开。
如何以编程方式区分箭头功能和常规功能?
答案 0 :(得分:4)
我能想到的最好的就是使用select id::text::int
from YourTable
cross join lateral
unnest(xpath('game/id/text()', col1)) xp(id)
:
toString
请参阅:
let isArrowFunction;
isArrowFunction = (fn) => {
console.log(fn.toString());
return fn.toString().indexOf('function') !== 0;
};
console.log(isArrowFunction(() => {}) === true);
console.log(isArrowFunction((foo: string) => {}) === true);
console.log(isArrowFunction(function () {}) === false);
答案 1 :(得分:2)
typeof (() => {}).prototype === "undefined"
true
,而:
typeof (function () {}).prototype === "undefined"
是false
,所以:
function isArrow(x)
{
return typeof (x.prototype) === "undefined"
}