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 4539558
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:57:46+00:00 2026-05-21T14:57:46+00:00

I have an object of type bar which has an Array of many foo

  • 0

I have an object of type bar which has an Array of many foos.

I want to be able to call a method of foo dynamically – I could do this with eval by passing a string, but I would rather get a handle on how to pass the function.

Am I – conceptually – doing this the right way?

var foo = function() {
    this.methodA = function() {
        return "a";
    };
    this.methodB = function() {
        return "b";
    };
};

var bar = function() {
    var foos = [];

    this.construct = function() {
        foos[0] = new foo();
    }; this.construct();

    this.callFoo = function(f) {
        return foos[0].f();
    };
};

b = new bar();
b.callFoo(foo.methodA); //<-- This doesn't work
b.callFoo(methodA); //<-- Or this
  • 1 1 Answer
  • 1 View
  • 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-21T14:57:47+00:00Added an answer on May 21, 2026 at 2:57 pm

    your leaking globals everywhere.

    // global leak
    foo = function() {
    
        // global leak
        methodA = function() {
            return "a";
        };
        // global leak
        methodB = function() {
            return "b";
        };
    };
    // global leak
    bar = function() {
        var foos = [];
        // global leak
        construct = function() {
            foos[0] = new foo();
        };construct();
    
        this.callFoo = function(f) {
            return foos[0].f();
        };
    };
    
    b = new bar();
    b.callFoo(foo.methodA); //<-- This doesn't work
    b.callFoo(methodA); //<-- Or this
    

    To answer the actual question try this.

    var foo = function() {
        return {
            methodA: function() { return "a"; },
            methodB: function() { return "b"; }
        };
    }
    
    var bar = function() {
        var foos = [];
    
        return {
            construct: function() {
                foos.push(foo());
            },
            callFoo = function(name) {
                return foos[0][name]();
            }
        }
    }
    
    b =  bar();
    b.callFoo("methodA");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class Foo which contains a datamember of type Bar . I
I have an object of type IMAGE which holds image. I wanted to display
I have an object of type list from which I wish to use to
I have an object of type Hash that I want to loop over via
I have an object of type X509Certificate2 and want to display it to the
I have an 'superior object' of type HierarchyNode which will have childnodes (each child
If I have a class, which has dual dependencies on the same type (needs
Given that I have a type Wibble.Wobble that exists in an assembly Foo.Bar And
So I have an object the inherits a list. public class Foo: List<Bar> And
I have an object value , which is of some type, either @XmlRootElement -annotated,

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.