Javascript 约翰·雷西格';s简单的类实例化和;使用“严格的”;

Javascript 约翰·雷西格';s简单的类实例化和;使用“严格的”;,javascript,ecmascript-5,use-strict,Javascript,Ecmascript 5,Use Strict,参考: 我想知道,是否有任何兼容ECMAScript 5的方法来实现相同的功能。问题是,访问参数。被调用方在严格模式下不推荐使用。据我所知参数。被调用方在严格模式下不推荐使用,在这种情况下,您可以继续使用它;相反,它已被删除,并且尝试使用将(或应该)引发异常 解决方法是使用命名匿名函数,如果您不介意这种矛盾修饰法的话。我真的应该说。例如: function someFunc(){ return function funcExpressionName(args){ if (this i

参考:


我想知道,是否有任何兼容ECMAScript 5的方法来实现相同的功能。问题是,访问
参数。被调用方在严格模式下不推荐使用。

据我所知
参数。被调用方在严格模式下不推荐使用,在这种情况下,您可以继续使用它;相反,它已被删除,并且尝试使用将(或应该)引发异常

解决方法是使用命名匿名函数,如果您不介意这种矛盾修饰法的话。我真的应该说。例如:

function someFunc(){
  return function funcExpressionName(args){
    if (this instanceof funcExpressionName) {
      // do something
    } else
      return new funcExpressionName( arguments );
  };
}
在我的示例中,您提供的名称
funcExpressionName
不应该从任何地方访问,除非它应用于函数内部,但不幸的是IE有其他想法(如果您愿意,您可以看到)


对于您问题中的示例,我不确定如何处理
参数.callee
,因为我不知道调用函数是如何设置的,但是
参数.callee
的使用将按照我的示例被替换。

John Resig的原始代码由于无参数构造函数而失败

var Timestamp = makeClass();
Timestamp.prototype.init = function() {
    this.value = new Date();
};

// ok
var timestamp = Timestamp();
alert( timestamp.value );  

// TypeError: args is undefined
var timestamp = new Timestamp();
alert( timestamp.value );   
但可以使用以下行进行修复

this.init.apply( this, args && args.callee ? args : arguments );
NNNN给出的上述想法相当不错。为了避免IE问题,我建议以下解决方案

function makeClassStrict() {
    var isInternal, instance;

    var constructor = function(args) {
        // Find out whether constructor was called with 'new' operator.
        if (this instanceof constructor) {
            // When an 'init' method exists, apply it to the context object.
            if (typeof this.init == "function") {
                // Ask private flag whether we did the calling ourselves.
                this.init.apply( this, isInternal ? args : arguments ); 
            }
        } else {
            // We have an ordinary function call.

            // Set private flag to signal internal instance creation.
            isInternal = true;                                           
            instance = new constructor(arguments);
            isInternal = false;                                         
            return instance;
        }
    };

    return constructor;
}
请注意,我们如何通过使用内部标志避免在
//do something
部分中引用
args.callee

function makeClassStrict() {
    var isInternal, instance;

    var constructor = function(args) {
        // Find out whether constructor was called with 'new' operator.
        if (this instanceof constructor) {
            // When an 'init' method exists, apply it to the context object.
            if (typeof this.init == "function") {
                // Ask private flag whether we did the calling ourselves.
                this.init.apply( this, isInternal ? args : arguments ); 
            }
        } else {
            // We have an ordinary function call.

            // Set private flag to signal internal instance creation.
            isInternal = true;                                           
            instance = new constructor(arguments);
            isInternal = false;                                         
            return instance;
        }
    };

    return constructor;
}