0

I was going over Javascript the good parts and I came across this example, where the author was trying to explain how to be able to call a superclass:

Object.method('superior', function (name) {
    var that = this,
        method = that[name];
    return function (  ) {
        return method.apply(that, arguments);
    };
});

an example of using this code:

super_get_name = that.superior('get_name');

however chrome wouldn't recognize method on Object. I tried doing the same thing using defineProperty but that didn't work either.. any help?

update: does this method cause any known conflicts with jQuery? As soon as i put the following at the first js file in my page:

I get this error in line 2062 in jquery-1.11.0.js:

Uncaught TypeError: Object function (name) {
    var that = this,
        method = that[name];
    return function (  ) {
        return method.apply(that, arguments);
    };
} has no method 'exec' 

this is the effected code:

    // Filters
    for ( type in Expr.filter ) {
        if ( (match = matchExpr[ type ].exec( soFar )) && (!preFilters[ type ] ||
            (match = preFilters[ type ]( match ))) ) {
            matched = match.shift();
            tokens.push({
                value: matched,
                type: type,
                matches: match
            });
            soFar = soFar.slice( matched.length );
        }
    }

any idea what's going on?

abbood
  • 23,101
  • 16
  • 132
  • 246

2 Answers2

4

The author of the book explains that for that piece of code to work you need to define the method function first:

Throughout the book, a method method is used to define new methods. This is its definition:

Function.prototype.method = function (name, func) {
 this.prototype[name] = func;
 return this;
};
Emilio Rodriguez
  • 5,709
  • 4
  • 27
  • 32
  • So that means one should actually never use `method` on `Object` to [avoid messing up the `Object.prototype`](http://stackoverflow.com/q/13296340/1048572)? – Bergi Apr 07 '14 at 09:58
  • @Bergi: Your question is unclear. `Object` has no method `method` by default. So to use it, you have to "mess" with the prototype. As the other question says: You need to be aware that people are changing the prototype, so use the proper loop construct. – Aaron Digulla Apr 07 '14 at 10:03
  • `method` is a `Function` method, which means it does not mess up `Object.prototype`. Only applying it to `Object` will… As my answer to the other question says, changing the loop construct is *not* the solution. – Bergi Apr 07 '14 at 10:13
2

seems that using that messes up my jquery.. is there a way to avoid it?

Yes. Redefine Function.prototype.method to

Function.prototype.method = function (name, func) {
  Object.defineProperty(this.prototype, name, {
    configurable: true,
    enumerable: false, // sic!
    writable: true,
    value: func
  });
  return this;
};

before using it on the Object function (Function.prototype.method itself doesn't mess up jQuery, but calling Object.method() does). See also How to define method in javascript on Array.prototype and Object.prototype so that it doesn't appear in for in loop.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • looks like u don't have an 80K+ score on SO for nothin'! anyways can you recommend me a *good* tutorial on how to call superclasses in javascript? been the very flexible language it is, it seems like everyone and their sister-in-law has an opinion on how to do anything.. I want something that's clear and time tested. The example in `javascript the good parts` is very elementary and basic, and doesn't fully address my situation. – abbood Apr 07 '14 at 10:19
  • 1
    I tend to `call` it explicitly, as [laid out here](http://stackoverflow.com/a/16963189/1048572) :-) Works 100%, is flexible, yet a bit lengthy and could use some caching optimistions. – Bergi Apr 07 '14 at 10:27