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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:24:53+00:00 2026-05-22T15:24:53+00:00

So, I got two objects, a and b. Now, I want to pass one

  • 0

So, I got two objects, a and b. Now, I want to pass one of b’s methods to the a object, wich should store it. Let’s call that method b.met:

b.met=function(){
    alert(this.txt);
}

Now, I want to call b.met from a. The following code doesn’t work, since a.met is a clone of b.met inside the scope of a:

a.met=b.met;
a.met(); //Executes in the 'a' scope!

The only way I found, by now, is to save the method’s name in a string and use it in an eval statement:

a.toCall='b.met';
eval(a.toCall+'();');

Since everyone says you should avoid using eval… what other possibilities are there?


EDIT – see in comments: So I changed my code from:

a:{
    processes:[],
    spawnProcess:function(type,id,closeFn){
    var closeFn=closeFn || 'function(){}';
    this.processes.push({type:type,id:id,closeFn:closeFn});
}

to:

a:{
    processes:[],
    spawnProcess:function(type,id,closeFn){
    var closeFn=function(){closeFn()} || 'function(){}';
    this.processes.push({type:type,id:id,closeFn:function(){closeFn()}});
}

When executing the following code, I get a too much recursion error:

a.spawnProcess('','',b.met);
a.processes[0].closeFn();
  • 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-22T15:24:53+00:00Added an answer on May 22, 2026 at 3:24 pm

    Your storing a reference to a function. A function is just a function. It get’s it’s definition for this based on the calling context.

    So if you store a.met = b.met and then call a.met() inside that function this === a

    Read up at the JavaScript garden about this

    What you want to do is store the function and the context to call it in.

    This can be done either as

    a.met = function() {
      b.met();
    }
    

    or

    a.met = b.met.bind(b);

    .bind requires ES5. Recommended cross browser alternatives include _.bind and $.proxy

    Edit

    You need to change

    a.spawnProcess('','',b.met);

    to

    a.spawnProcess('','', function() {
        b.met();
    });
    

    You probably also want this instead

    a: {
        processes: [],
        spawnProcess: function(type, id, closeFn) {
            this.processes.push({
                type: type,
                id: id,
                closeFn: closeFn || function() {}
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got two CMDlets that return lists of objects. One returns objects of the
I've got a polymorphic array of objects which implement two (informal) interfaces. I want
Say I've got two scheduled processes: A and B. Given that B should not
I've got two ListBox's with objects as an ItemsSource populating them. Right now, I'm
Let´s say I´ve got the following xml string: <Item Name=\$Resources:myresource,somestring;\</Item> Now I want to
Suppose I've got a generic MyClass<T> that needs to compare two objects of type
I've got two SVN branches (eg development and stable) and want to switch from
So I'm creating my domain objects for my Spring Roo app. I've got two
I want to convert a List to a List so that each object on
I'm trying to provide a functionality of having two Methods one called StartTask(action mymethod)

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.