Jee*_*Kim 7 javascript use-strict ecmascript-5
参考:http://ejohn.org/blog/simple-class-instantiation/
// makeClass - By John Resig (MIT Licensed)
function makeClass(){
return function(args){
if ( this instanceof arguments.callee ) {
if ( typeof this.init == "function" )
this.init.apply( this, args.callee ? args : arguments );
} else
return new arguments.callee( arguments );
};
}
Run Code Online (Sandbox Code Playgroud)
我想知道,如果有任何符合ECMAScript 5的方式来实现相同的功能.问题是,访问arguments.callee在严格模式下已弃用.
据我了解,它在严格模式下arguments.callee并未被弃用,在这种情况下您可以继续使用它;相反,它已被删除,并且尝试使用将(或应该)抛出异常。
如果您能原谅这种矛盾的话,解决方法是使用命名匿名函数。我真的应该说“命名函数表达式”。一个例子:
function someFunc(){
return function funcExpressionName(args){
if (this instanceof funcExpressionName) {
// do something
} else
return new funcExpressionName( arguments );
};
}
Run Code Online (Sandbox Code Playgroud)
在我的示例中,您提供的名称funcExpressionName不应该从任何地方访问,除了它所适用的函数内部,但不幸的是 IE 有其他想法(正如您通过Google 搜索可以看到的那样)。
对于您问题中的示例,我不确定如何处理,args.callee因为我不知道调用函数是如何设置的,但arguments.callee根据我的示例,将替换使用。