SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,047
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)

    Passing constructor arguments between child and super "class" properly

    Code:
    function BaseController(el) {
    
    	var that = this;
    
    	this.defaultAction = function() {
    		alert('BaseController el val: ' + el); // el is not defined...
    	};
    
    }
    
    function StoryController(el) {
    
    	var that = this;
    	
    	// call super constructor
    	BaseController.apply(that,arguments); // even though I am passing el it is not defined in the super class
    
    	this.defaultAction = function() {
    	
    		alert('StoryController el val: ' + el); // el is defined
    		StoryController.prototype.defaultAction.apply(that);
    	
    	};
    
    }
    
    StoryController.prototype = new BaseController(); // this is the problem, how can I do this without creating an instance, considering I can't pass the constructor arguments at this point?
    StoryController.prototype.constructor = StoryController;
    
    // -----------------------------------------------------------------
    
    var story = new StoryController('one');
    
    story.defaultAction();
    What is the proper method here so that el will be defined in the super "class".? I know I saw something on stack overflow a couple of days ago but can not seem to find it again.

    admin: Something is up with the code tags, I have no idea why the system is double posting the code. Does HIGHLIGHT=JS not exists anymore? – That seemed not be working.
    The only code I hate more than my own is everyone else's.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,047
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    bump

    This is the stack overflow thread I was referring to. However, the implementation shown will not work given the methods are not part of the functions prototype. therefore, I have no way to access the super method unless I make it a prototype. In which case I can't have it access el unless I assign it as a property.

    I'm starting to think this is not possible unless el is assigned as a property of the instance, huh…
    The only code I hate more than my own is everyone else's.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,047
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Alright, I don't think this is even possible from my research. I will just need to assign el to a publicly accessible property, which I guess is fine.
    The only code I hate more than my own is everyone else's.


Bookmarks

Posting Permissions

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