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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:53:06+00:00 2026-05-15T00:53:06+00:00

I’m trying to invoke addEventListener() using apply() method. The code is like: function rewrite(old){

  • 0

I’m trying to invoke addEventListener() using apply() method. The code is like:

function rewrite(old){
    return function(){
        console.log( 'add something to ' + old.name );
        old.apply(this, arguments);
    }
} 
addEventListener=rewrite(addEventListener);

It doesn’t work. The code works for normal JavaScript method, for example,

function hello_1(){
    console.log("hello world 1!");
}
hello_1=rewrite(hello_1);

Need help!

Thanks!

  • 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-15T00:53:07+00:00Added an answer on May 15, 2026 at 12:53 am

    You can’t count on addEventListener being a real Javascript function, unfortunately. (This is true of several other host-provided functions, like window.alert). Many browsers do the Right Thing(tm) and make them true Javascript functions, but some browsers don’t (I’m looking at you, Microsoft). And if it’s not a real Javascript function, it won’t have the apply and call functions on it as properties.

    Consequently, you can’t really do this generically with host-provided functions, because you need the apply feature if you want to pass an arbitrary number of arguments from your proxy to your target. Instead, you have to use specific functions for creating the wrappers that know the signature of the host function involved, like this:

    // Returns a function that will hook up an event handler to the given
    // element.
    function proxyAEL(element) {
        return function(eventName, handler, phase) {
            // This works because this anonymous function is a closure,
            // "closing over" the `element` argument
            element.addEventListener(eventName, handler, phase);
        }
    }
    

    When you call that, passing in an element, it returns a function that will hook up event handlers to that element via addEventListener. (Note that IE prior to IE8 doesn’t have addEventListener, though; it uses attachEvent instead.)

    Don’t know if that suits your use case or not (if not, more detail on the use case would be handy).

    You’d use the above like this:

    // Get a proxy for the addEventListener function on btnGo
    var proxy = proxyAEL(document.getElementById('btnGo'));
    
    // Use it to hook the click event
    proxy('click', go, false);
    

    Note that we didn’t pass the element reference into proxy when we called it; it’s already built into the function, because the function is a closure. If you’re not familiar with them, my blog post Closures are not complicated may be useful.

    Here’s a complete example:

    <!DOCTYPE HTML>
    <html>
    <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
    <title>Test Page</title>
    <style type='text/css'>
    body {
        font-family: sans-serif;
    }
    #log p {
        margin:     0;
        padding:    0;
    }
    </style>
    <script type='text/javascript'>
    
        window.onload = pageInit;
        function pageInit() {
            var proxy;
    
            // Get a proxy for the addEventListener function on btnGo
            proxy = proxyAEL(document.getElementById('btnGo'));
    
            // Use it to hook the click event
            proxy('click', go, false);
        }
    
        // Returns a function that will hook up an event handler to the given
        // element.
        function proxyAEL(element) {
            return function(eventName, handler, phase) {
                // This works because this anonymous function is a closure,
                // "closing over" the `element` argument
                element.addEventListener(eventName, handler, phase);
            }
        }
    
        function go() {
            log('btnGo was clicked!');
        }
    
        function log(msg) {
            var p = document.createElement('p');
            p.innerHTML = msg;
            document.getElementById('log').appendChild(p);
        }
    
    </script>
    </head>
    <body><div>
    <input type='button' id='btnGo' value='Go'>
    <hr>
    <div id='log'></div>
    </div></body>
    </html>
    

    Regarding your question below about func.apply() vs. func(), I think you probably already understand it, it’s just that my original wrong answer confused matters. But just in case: apply calls function, doing two special things:

    1. Sets what this will be within the function call.
    2. Accepts the arguments to give to the function as an array (or any array-like thing).

    As you probably know, this in Javascript is quite different from this in some other languages like C++, Java, or C#. this in Javascript has nothing to do with where a function is defined, it’s set entirely by how the function is called. You have to set this to the correct value each and every time you call a function. (More about this in Javascript here.) There are two ways to do that:

    • By calling the function via an object property; that sets this to the object within the call. e.g., foo.bar() sets this to foo and calls bar.
    • By calling the function via its own apply or call properties; those set this to their first argument. E.g., bar.apply(foo) or bar.call(foo) will set this to foo and call bar.

    The only difference between apply and call is how they accept the arguments to pass to the target function: apply accepts them as an array (or an array-like thing):

    bar.apply(foo, [1, 2, 3]);
    

    whereas call accepts them as individual arguments:

    bar.apply(foo, 1, 2, 3);
    

    Those both call bar, seting this to foo, and passing in the arguments 1, 2, and 3.

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

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is not pretty but it works: rm -R $(ls… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer Yes. Override the base1 and base2 methods in Derived to… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer No, you can't. Unfortunately, UIEvent doesn't expose any public way… May 16, 2026 at 12:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and

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.