[ 3 posts ]

Daniel Ji

  • Username: humblepie
  • Joined: Tue Feb 28, 2012 10:30 am
  • Posts: 147
  • GitHub: humblepie
  • Gists: humblepie
  • Offline
  • Profile

parent class reference with Y.Base.create()

Post Posted: Tue Jul 31, 2012 12:35 pm
+0-
Hi everyone,

Is there a way to get to the parent class' method while inside a subclass method that is overriding the parent method?

In the java world, you can call super(), which can invoke the parent classes method first. Is there something similar I can do with the class that is created from YUI's Y.Base.create()?

If I can't have a parent reference with Y.Base.create(), then what would be the best way to do this?

Thanks.

Luke Smith

YUI Contributor

  • Username: lsmith
  • Joined: Thu Aug 28, 2008 7:50 am
  • Posts: 511
  • Location: Sunnyvale
  • Twitter: ls_n
  • GitHub: lsmith
  • Gists: lsmith
  • IRC: ls_n
  • YUI Developer
  • Offline
  • Profile
Tags:

Re: parent class reference with Y.Base.create()

Post Posted: Tue Jul 31, 2012 4:41 pm
+0-
The subclass constructor function will have a static `superclass` property which points to the superclass's prototype, so you can reference superclass methods from there. Note, you have to invoke them via call/apply.

Code:
SubClass.prototype.doMagicStuff = function (toads, batWings, fairyDust) {
    var potion = SubClass.superclass.doMagicStuff.call(this, toads, batWings);

    potion.addIngredient(fairyDust);

    return potion;
};

Daniel Ji

  • Username: humblepie
  • Joined: Tue Feb 28, 2012 10:30 am
  • Posts: 147
  • GitHub: humblepie
  • Gists: humblepie
  • Offline
  • Profile
Tags:

Re: parent class reference with Y.Base.create()

Post Posted: Tue Jul 31, 2012 5:03 pm
+0-
Hi Luke,

While in debugging mode, I was looking for the static superclass property on the class object and didn't see it. But it makes sense now after reading up on static properties/methods for JS :)

Thanks for the tip on using call/apply. I saw similar syntax elsewhere when the superclass property was used but didn't think much of it at the time.

-Daniel
  [ 3 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum