1

I have some code in jsFiddle here: http://jsfiddle.net/xyuY6/

And the code:

var App= {};

App.WebPage = {
 WebPage : function(name) {
     alert("In Super");
     this.name = name;  
 },

 Home : function(name) {
      alert("In Sub");
      App.WebPage.WebPage.call(this, name);
 }


};

App.WebPage.WebPage.prototype.sayName = function() {
alert(this.name);
};

inherit(App.WebPage.WebPage, App.WebPage.Home);

var x = new App.WebPage.Home("zack");
x.sayName();

function inherit(subType, superType) {
    alert("IN HERE");
    var p = Object.create(superType);
    p.constructor = subType;
    subType.prototype = p;
}

What I'm trying to do is create a App namespace with two constructors in this namespace, WebPage and Home. WebPage is a "super type" and Home is the "sub type". Home should inherit everything from WebPage.

The problem though, as you'll see when you run the JavaScript, is the sayName function defined on the WebPage prototype is not being called when I do x.sayName. Thus, it's not being inherited.

Why?

Thanks for the help!!!

2 Answers 2

2

It seems like you got the superclass and subclass mixed up at some point (either in the call or in inherit's definition)...

Here's the fixed code (if I understand what you are trying to do): http://jsfiddle.net/45uLT/

var App= {};

App.WebPage = {
     WebPage : function(name) {
         alert("In Super");
         this.name = name;  
     },

     Home : function(name) {
          alert("In Sub");
          App.WebPage.WebPage.call(this, name);
     }


};


App.WebPage.WebPage.prototype.sayName = function() {
    alert(this.name);
};

inherit(App.WebPage.WebPage, App.WebPage.Home);

var x = new App.WebPage.Home("zack");
x.sayName();

function inherit(superType, subType) {
    alert("IN HERE");
    var p = new superType;
    subType.prototype = p;
    subType.prototype.constructor = subType;
}

Hope this is what you wanted.

Sign up to request clarification or add additional context in comments.

3 Comments

I updated the jsFiddle. Trying to override the sayName method. Could you tell me why this isn't working? jsfiddle.net/45uLT/1
Never mind, just needed to change up the order of some calls.
Yup, you need to redefine the method after you inherit! :-) Glad I could help!
1

Here's a working example in jsfiddle based on what you're trying to do above:

http://jsfiddle.net/8GwRd/

var App = (function() {
    function WebPage(name) {
        this.type = 'WebPage';
        this.name = name;
    }

    WebPage.prototype.sayName = function() {
      alert(this.name + ' is a ' + this.type);
    };

    function Home(name) {
        WebPage.call(this, name);
        this.type = 'Home'
    }

    Home.prototype = new WebPage();

    return {
        WebPage: WebPage,
        Home: Home
    };       

})();

var y = new App.WebPage('bob');
y.sayName();

var x = new App.Home("zack");
x.sayName();

1 Comment

This approach is pretty different than mine. Is there a reason this approach is possibly better?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.