Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6847495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:45:05+00:00 2026-05-27T00:45:05+00:00

function Foo() { this.SayFoo = function() { console.log(‘Foo’); }; } function Bar() { this.SayBar

  • 0
function Foo() {
    this.SayFoo = function() {
        console.log('Foo');
    };
}

function Bar() {
    this.SayBar = function() {
        console.log('Bar');
    };
}

Foo.prototype = new Bar();

var fooBar = new Foo();
fooBar.SayBar();

This obviously works, but is it the correct way to do it?

Is there any way to make use of jQuery’s $.extend or something similar in order to achieve the same inheritance results?

Including other frameworks besides jQuery is not an option in this case.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:45:06+00:00Added an answer on May 27, 2026 at 12:45 am

    There are actually multiple ways to do inheritance in JavaScript: neoclassical, prototypal, and functional. Douglas Crockford has nothing but bad things to say about neoclassical inheritance—the method you have above, and the method most Java/C# developers think will be the most natural. The reason revolves around all the awkward things you have to do to get it right—setting the prototype, setting the constructor, etc. Also, setting the prototype to a new instance of the parent class, like you have above, is usually frowned upon strongly, I believe because it complicates handling parameters with the base ctor.

    If you’re really sold on the neoclassical method, here’s a great link that really goes over it.

    The key part I reproduce for you here:

    function Inherit(sub,super){
        var thinF = function(){};
        thinF.prototype = super.prototype;
        sub.prototype = new thinF();
        sub.prototype.constructor = sub;
        sub.super = super.prototype;
        if( super.prototype.constructor == Object.prototype.constructor ){
            super.prototype.constructor = super;
        }
    }
    

    FWIW Here’s an example of functional inheritance that also highlights something you don’t get with the neoclassical method: encapsulation/information hiding.

    function eventRaiser(protectedStuff) {
        protectedStuff = protectedStuff || {}; 
        var that = {};
        var events = {};  //private
    
        protectedStuff.raise = function(key) {
            if (!events[key]) return;
                for (var i = 0; i < events[key].funcs.length; i++)
                    events[key].funcs[i].apply(null, Array.prototype.slice.call(arguments, 1));
        };
    
        that.subscribe = function(key, func) {
            if (!events[key])
                events[key] = { name: key, funcs: [] };
            events[key].funcs.push(func);
        };
    
        return that;
    }        
    
    function widget() {
        var protectedStuff = {};
        var that = eventRaiser(protectedStuff);
    
        that.doSomething = function() { 
            alert("doing something"); 
            protectedStuff.raise("doStuffEvent");
        };
    
        return that;
    }
    
    $(function() {
        var w = widget();
        w.subscribe("doStuffEvent", function(){ alert("I've been raised"); });
        w.doSomething();
    
        w.protectedStuff.raise("doStuffEvent"); //error!!!!!  raise is protected
        w.raise("doStuffEvent"); //and this obviously won't work
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

var objs = new Array(); function Foo(a) { this.a = a $(#test).append($(<button></button>).html(click).click(this.bar)); } Foo.prototype.bar
If I have a function like this: function foo(_this) { console.log(_this); } function bar()
In this code: <?php class Foo { var $value; function foo($value) { $this->setValue($value); }
My question is: function Foo() { this.foo = bar; // <- What is this
Consider the following to JavaScript snippets: function foo() { this.bar = function() { };
I've got a JavaScript object, built this way: function foo() { this.length = 0;
How can I include foo() function of foo.c in this small program (sorry for
If I understand it correctly this means extern void foo(); that the function foo
I have some third-party Javascript that has statements like this: FOO = function() {
For example: > function foo() { > jQuery(whatever).each( function() { return; // this just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.