John Resig's simple class instantiation and “use strict”

断了今生、忘了曾经 提交于 2019-11-29 02:20:25

As I understand it arguments.callee isn't deprecated in strict mode, in which case you could continue to use it; rather, it has been removed and attempted use will (or is supposed to) throw an exception.

The workaround is to use named anonymous functions, if you'll forgive the oxymoron. Really I should say "named function expressions". An example:

function someFunc(){
  return function funcExpressionName(args){
    if (this instanceof funcExpressionName) {
      // do something
    } else
      return new funcExpressionName( arguments );
  };
}

The name you provide, in my example funcExpressionName is not supposed to be accessible from anywhere except inside the function it applies to, but unfortunately IE has other ideas (as you can see if you Google it).

For the example in your question I'm not sure how to handle the args.callee since I don't know how that is set by the calling function, but the use of arguments.callee would be replaced as per my example.

The above idea given by nnnnnn is quite good. And in order to avoid IE issues I suggest the following solution.

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;
}

Note how we avoid reference to args.callee in the // do something part by using an internal flag.

John Resig's original code fails with a parameterless constructor.

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 );   

But it can be repaired using the following line

this.init.apply( this, args && args.callee ? args : arguments );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!