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?

2
  • 1
    Somewhere in that book he does define a custom method function. Use it! Commented Apr 7, 2014 at 9:53
  • @Bergi seems that using that messes up my jquery.. is there a way to avoid it? Commented Apr 7, 2014 at 10:09

2 Answers 2

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;
};
Sign up to request clarification or add additional context in comments.

3 Comments

So that means one should actually never use method on Object to avoid messing up the Object.prototype?
@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.
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.
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.

2 Comments

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.
I tend to call it explicitly, as laid out here :-) Works 100%, is flexible, yet a bit lengthy and could use some caching optimistions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.