简体   繁体   中英

TypeError: Result of expression near '…}.bind(this))…' [undefined] is not a function

I am getting a Safari only error: TypeError: Result of expression near '...}.bind(this))...' [undefined] is not a function.

These are lines 88-92:

$(this.options.work).each(function(i, item) {
  tmpItem = new GridItem(item);
  tmpItem.getBody().appendTo($("#" + this.gridId));
  this.gridItems.push(tmpItem);
}.bind(this));

Any ideas what is causing this?

Older versions of Safari don't support bind . If you try this ( http://jsfiddle.net/ambiguous/dKbFh/ ):

console.log(typeof Function.prototype.bind == 'function');

you'll get false in older Safaris but true in the latest Firefox and Chrome. I'm not sure about Opera or IE but there is a compatibility list (which may or may not be accurate):

http://kangax.github.com/es5-compat-table/

You can try to patch your own version in with something like this :

Function.prototype.bind = function (bind) {
    var self = this;
    return function () {
        var args = Array.prototype.slice.call(arguments);
        return self.apply(bind || null, args);
    };
};

but check if Function.prototype.bind is there first.

or for a bind shim supporting partial application:

if (!Function.prototype.bind) {
    Function.prototype.bind = function(o /*, args */) {
        // Save the this and arguments values into variables so we can // use them in the nested function below.
        var self = this, boundArgs = arguments;
        // The return value of the bind() method is a function 
        return function() {
            // Build up an argument list, starting with any args passed
            // to bind after the first one, and follow those with all args // passed to this function.
            var args = [], i;
            for(i = 1; i < boundArgs.length; i++) args.push(boundArgs[i]); 
            for(i = 0; i < arguments.length; i++) args.push(arguments[i]);
            // Now invoke self as a method of o, with those arguments
            return self.apply(o, args); 
        };
    }; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM