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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:50:34+00:00 2026-05-13T10:50:34+00:00

Problem & Reason One of my team mate ended up in messy situtaion implementing

  • 0

Problem & Reason

One of my team mate ended up in messy situtaion implementing function hooking in javascript. this is the actual code

 function ActualMethod(){   
       this.doSomething = function() {
              this.testMethod();
        };

       this.testMethod = function(){
            alert("testMethod");
        };
   }


 function ClosureTest(){
       var objActual= new ActualMethod();   
    var closeHandler = objActual.doSomething;   
    closeHandler();     
    closeHandler.apply(objActual,arguments); //the fix i have added
    this.ActualTest = function() {
        alert("ActualTest");
        };
  }

In the above code, var closeHandler is created in the context of ClosureTest(), but it holds the handler of the ActualMethod.doSomething. Whenever calling the closeHandler() ended up in “object doesnt support this method” error.

This is because doSomething() function calls another method inside called this.testMethod();. Here this refers to the context of the caller not callee.so i assume the closeHandler is bound to the environment(ClosureTest) actually created.Even though it holds the handler to the another context, it just exposes the properties of its own context.

Solution

To avoid this i suggest to use apply to specify the conext in which it needs to execute.

closeHandler.apply(objActual,arguments);

Questions

is it perfect scenario for closures..??

What are the intersting places you have encountered closures in javascript..?

UPDATE

Yes its simple i can call the method directly. but the problem is, in a particular scenario I need to intercept the call to actuall method and run some code before that, finally execute the actual method..

say for an example, am using 3rd party aspx grid library, and all the mouseclick events are trapped by their controls. In particular group by mouse click i need to intercept the call to their ilbrary method and hook my mthod to execute instead and redirect the call to actual library method

hope this helps

  • 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-13T10:50:34+00:00Added an answer on May 13, 2026 at 10:50 am

    Update: Because you probably left out some details in your code, it is difficult to adapt it into something workable without missing the point of your actual code. I do think I understand your underlying problem as you describe it. I hope the following helps.

    Suppose the following simple example:

    // Constructor function.
    function Example() {
        // Method:
        this.method = function() {
            alert("original method");
        }
    }
    
    // You would use it like this:
    var obj = new Example();
    obj.method(); // Calls original method.
    

    To intercept such a method call, you can do this:

    function wrap(obj) {
        var originalMethod = obj.method;
        obj.method = function() {
            alert("intercepted call");
            originalMethod.apply(this, arguments);
        }
        return obj;
    }
    
    var obj = wrap(new Example());
    obj.method(); // Calls wrapped method.
    

    Unfortunately, because method() is defined in the constructor function, not on a prototype, you need to have an object instance to wrap the object.


    Answer to original question: The doSomething() function is used as a method on objects created with ActualMethod(). You should use it as a method, not detach it and use it as a function in a different context. Why don’t you just call the method directly?

    function ClosureTest(){
        var objActual = new ActualMethod();
        // Call method directly, avoid messy apply() calls.
        objActual.doSomething();
        this.ActualTest = function() {
            alert("ActualTest");
        };
    }
    

    If you assign a method (a function on some object) to a local variable in Javascript and call it, the context will be different (the value of this changes). If you don’t want it to happen, don’t do it.

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

Sidebar

Related Questions

I am trying to resolve Euler Problem 18 -> http://projecteuler.net/index.php?section=problems&id=18 I am trying to
[update2] One of those problems I solved & can't remember how... :) [update1] could
Problem: I have two spreadsheets that each serve different purposes but contain one particular
Problem (simplified to make things clearer): 1. there is one statically-linked static.lib that has
This Jquery problem has been bugging me for a while now. I developed a
This is a strange problem but it happens enough that I wanted to ask.
EDIT: Problem solved. This was (yet another) situation where the problem wasn't really where
I'm looking for a way to boost my team's productivity, and one way to
I'm having this weird problem: when my program reach this method: //Returns the transpose
I have taken this example test before I'll try to take the real one

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.