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

  • Home
  • SEARCH
  • 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 6723653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:35:40+00:00 2026-05-26T09:35:40+00:00

When I run this code: function foo() { console.log(foo); var self = this; this.a

  • 0

When I run this code:

function foo() { 
    console.log("foo"); 
    var self = this; 
    this.a = function() { 
        return arguments.length == 0 ? self.b : (self.b = arguments[0]); 
    }; 
}; 
function bar() {
}; 
bar.prototype = new foo(); 
var a = new bar(); 
var b = new bar(); 
console.log(a.a()); 
b.a(true); 
console.log(a.a());

I get the following output: foo, undefined, true

What I do not understand is how the object returned by the constructor function foo somehow manages to allocate a new memory location for b. I was totally expecting the last output to be true because I only get 1 foo output.

That this works is great, but it feels too much like magic.

My problem really here is that I’d like to setup an initialization order (or chain). What I’ve realized is that foo cannot take any arguments, because it’s only called once, to provide a base (or template of sorts).

I’d like to devise a really simple schema where foo has parameters that has to be passed from bar to foo (because bar inherits foo). I’m fine with something like calling foo.apply(this, arguments) from bar but the call to set the prototype has to be done without arguments, what I need is a fluent way to somehow tell the two apart.

But I really don’t want to build an entire service around creating objects that allow some kind of inheritance, the only thing I really care about is constructing the objects, correctly…

  • 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-26T09:35:41+00:00Added an answer on May 26, 2026 at 9:35 am

    On a side note, you’re sometimes using self and sometimes using this… personally, I’d be more consistent. I like to use that because this can be confusing and self is part of the BOM, but it’s just a matter of taste.

    Try parasitic inheritance:

    function foo(arg) {
        var that = this;
        that.a = function() { 
            return arguments.length == 0 ? that.b : (that.b = arguments[0]); 
        };
    }
    
    function bar(arg) {
        return new foo(arg);
    }
    
    bar.prototype = new foo(); 
    var a = new bar();  // the `new` is now optional. Personally I'd remove it...
    var b = bar();  // ... like I did here!
    console.log(a.a()); 
    b.a(true); 
    console.log(a.a());
    console.log(b.a());
    

    … here the inheritance behaves in a more classically, explicitly invoking a parent constructor when the constructor is called.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I run this code - var html= '<html><head></head><body><div class=bar></div></body></html>'; console.log($(html).find('div')); I get no
On document ready I run this code: jQuery(document).ready(function(){ jQuery('#button').click(function() { jQuery('#contact_form').load(/Users/mge/Downloads/jquery-ajax-1/readme.txt); return false; });
Taking the jQuery framework for example, if you run code like this: $(document).ready(function init()
Consider this code: function Foo(ds as OtherDLL.BaseObj) dim lngRowIndex as long dim lngColIndex as
Why this code don't work,when i want run this code vwd 2008 express show
When I run this code the selected item is not visible. I've already tried
when i run this code : CONADefinitions.CONAPI_FOLDER_INFO2 FolderInfo; int iResult = 0; IntPtr Buffer
When I run this code, About half-way through the concatenation loop, $xml becomes null
I'm trying to run this code: let coins = [50, 25, 10, 5, 2,1]
When I run this code url = ('http://maps.google.com/maps/nav?'+ 'q=from%3A'+from_address+ '+to%3A'+to_address+ '&output=json&oe=utf8&key='+api_key) request = urllib2.Request(url)

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.