Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Inheritance and function overriding question

    Hi!

    This is probably a classic inheritance thing...

    I have two objects, C1 and C2. Both contains a callback method named 'callback'. Now, if I let C2 inherit from C1, then C1's 'callback' gets overridden by C2's. The problem is I still want C1's methods to access their own 'callback' method.

    Is this possible, and is it "valid"? Am I headed down disaster lane here? Should I re-think and refactor?


    Example:

    Code:
    function C1 () {};
    
    C1.prototype.callback = function () {
    	console.log('c1 called');
    };
    
    C1.prototype.call = function () {
    	//do stuff
    	this.callback();
    };
    
    function C2 () {};
    
    C2.prototype = new C1();
    
    C2.prototype.callback = function () {
    	console.log('c2 called');
    };
    
    
    var obj = new C2();
    obj.call();
    Output:

    c2 called

    Regards

    Don

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    The usual way to do this is to set up way to reference the C1 prototype from within C2

    eg.

    C2.uber = C1.prototype;

    You would then be able to use C2.uber in place of C2 whenever you want to access methods of C1 that were overwritten by methods in C2.

    obj.uber.call();
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah, of course!

    Since I'd like my parent classes to be "autonomous", I tried changing the C1 call method like this:

    Code:
    C1.prototype.call = function () {
    	//do stuff
            C1.prototype.callback();
    };
    It seems to work the way I want, but is it good practice?

    In reality the callback is being called back by external API. The good thing (it seems) using this approach, is that I don't need to wrap the callback in a function
    Code:
    extAPIcall ( function () { C1.prototype.callback(response); } )
    since the prototype is (kind of) static. I suppose, in a way, this could be some what compared with doing a Class::function() call in other languages.
    Last edited by donfuego; 01-16-2012 at 09:09 AM.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    The main thing to remember is that JavaScript is a prototyping language and not an object oriented language - so attempting to use OO techniques the same way you would in an OO language results in far less efficient and messier code than if you make proper use of JavaScript's prototyping features.

    All objects exist as objects in JavaScript - each is effectively the singleton static example of its own class (as classes do not exist in JavaScript). So there is nothing wrong with C1.prototype.callback where you know that it is the C1 version you want to run.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for clarifying. I'm still trying to get my head around it though. I have my roots in old school procedural programming, and OO is still a bit alien to me. JS, in some ways, reminds me of the procedural past, but with an OO approach...or something. Anyhow, it makes my head spin!

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    as of late (5th edition) you can also access the prototype chain directly with Object.getPrototypeOf(obj) instead of obj.prototype
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •