tags:

views:

31

answers:

1

Hi there,

When using JQuery, extending an object that has an overridden toString() function causes an "Operation is not supported" error in Firefox. However in Chrome it works fine. Is this a bug in JQuery or am I doing something wrong in the below code snippet?

    var foo = function () {
        var that = this;

        that.toString = function () { return "foobar" };

        return that;
    }();

    var foo2 = function () {
        var that = this;            

        that = $.extend(true, {}, foo); // copy = options[ name ]; = "Operation is not supported" in Firefox 3.6.8

        return that;
    } ();

    alert(foo.toString()); //"foobar" in Chrome
    alert(foo2.toString()); //"foobar" in Chrome

JQuery version 1.4.2

Many thanks,

Godders

+1  A: 

When you call the anonymous function to get the value for "foo", the this variable will be referencing the window object. Same goes for the anonymous function you call for "foo2". Thus, you're trying to extend the window object. Is that really what you want to do?

edit what Firefox seems to be tripping over is the attempt to copy the "sessionStorage" attribute of window. Try adding this line:

var test = window['sessionStorage'];

and you'll get the exact same error.

Pointy
@Pointy: Thanks for the answer. This has prompted me to go back and re-read the 4 Invocation patterns in Crockford's "Good Parts" book.
Godders
@Pointy: Ok, so what I wanted to do requires var that = {}; in both the foo and foo2 functions. Thanks for the help.
Godders
You're welcome! Good luck!
Pointy