给定一个函数:
function x(arg) { return 30; }
你可以称之为两种方式:
result = x(4);
result = new x(4);
第一个返回30,第二个返回一个对象。
How can you detect which way the function was called 在函数本身内部?
无论您的解决方案是什么,它都必须与以下调用一起工作:
var Z = new x();
Z.lolol = x;
Z.lolol();
All the solutions currently think the Z.lolol()
is calling it as a constructor.