TypeError: the result of the expression next to '...}. Bind (this)) ... '[undefined] is not a function

I only get the error Safari: TypeError: result of expression next to '...}. 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 causes this?

+4
source share
2 answers

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

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

you will get false in earlier versions of Safari, but true in recent versions of 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 fix your own version 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 exists.

+10
source

or for a bind pad that supports a 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); }; }; } 
+1
source

All Articles