最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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?

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?

Share Improve this question edited Oct 31, 2011 at 6:20 mu is too short 435k71 gold badges859 silver badges818 bronze badges asked Oct 31, 2011 at 5:57 DustinDustin 8,26711 gold badges35 silver badges45 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

Older versions of Safari don't support bind. If you try this (http://jsfiddle/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 patibility list (which may or may not be accurate):

http://kangax.github./es5-pat-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); 
        };
    }; 
}
发布评论

评论列表(0)

  1. 暂无评论