jquery - Javascript inheritance: overriding functionality with module pattern and $.extend -



jquery - Javascript inheritance: overriding functionality with module pattern and $.extend -

i have next situation, in extend parent class , override behavior. overridden behavior not called.

// parent scope definition (function ($) { $.extend(true, window, { demo: { parent: parent } }); function parent() { function a() { //do } function b() { //do else a(); } // public api $.extend(this,{ "a":a, "b":b }); } })(jquery); // kid scope definition (function ($,parent) { $.extend(true, window, { demo: { child: kid } }); function child() { // inherit parent parent.apply(child,arguments); this.a = function () { // override parent behavior } this.b(); } })(jquery,parent); //...somewhere else, in scope call: var kid = new child(); <html> <script> var kid = new child(); </script> </html>

when creating new kid object, b called, "this" pointing kid context. however, context of not child, global (window) context. overridden behavior not called either.

i have utilize module pattern (no prototypical inheritance) , override "superclass" behavior.

could please allow me know how can "preserve" context, in order create sure overridden behavior called.

thank in advance, andrei

edit #1: must utilize modules (and little alter "parent" possible when overriding behavior)

edit #2: more clarity, answers provided below, must utilize "revealing module pattern" parent module/object , extend/override behavior in child.

edit #3: seems question title , problem different , may misleading: handled , answered problems real core issue regarding javascript overriding in context of (revealing) module patter (as indicated in 1 of answers). scope alter (which occur) has been mistakenly considered real problem. removing mention form title. correction done others may benefit answers below. there scope change, normal in nowadays code design , in calling scenario. disregard aspect (context) in question.

when defining module

$.extend(this,{ "a":a, "b":b });

you implicitly using revealing module pattern defining , b in closure, using direct closure references "public" members, , passing object literal $.extend. revealing module pattern anti-pattern variant of basic module pattern because this variant unable pick overrides. if want pick overrides, general rule of thumb utilize this much possible.

rewriting illustration utilize this appropriate, next should trick:

(function parent() { function a() { //do } function b() { //use this! this.a(); } // public api $.extend(this,{ "a":a, "b":b }); })(); (function child() { // inherit parent parent.apply(child,arguments); this.a = function () { // override parent behavior } this.b(); })();

javascript jquery inheritance scope

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -