rhodee
rhodee

Reputation: 1267

Extend objects using functional invocation pattern. How?

Fiddle here: http://jsfiddle.net/rhodee/4NKVH/

I've been reading the Crockford book and was wondering what is a proven approach to prying open the base constructor object and adding a function to it that child objects can access?

I thought I could access the prototype of my object and it appears that is not possible given my current code.

Thanks for any ideas.

Upvotes: 0

Views: 56

Answers (1)

user123444555621
user123444555621

Reputation: 152976

You have several errors:

  • Your constructor function returns a totally unrelated object. Don't do that. Return this. (If no return statement exists, the constructor does this implicitly)
  • Superconstructor calls are not very intuitive in JS. You need the ugly construct MySuperclass.call(this, arg1, arg2, ...);, so in your case mammal.call(this, spec); in the cat class
  • You need the new keyword to instantiate an object that uses the prototype chain.

See http://jsfiddle.net/4NKVH/5/ for a fixed version of your code.

Upvotes: 1

Related Questions