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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:08:05+00:00 2026-06-07T12:08:05+00:00

Why are these two ways of writing a jQuery plugin equivalents? First way: $.fn.myplugin

  • 0

Why are these two ways of writing a jQuery plugin equivalents?


First way:

$.fn.myplugin = function () {
    return $(this).each(function() {
        return $(this).bind('click', function () {
            console.log('Hi');
        });
    });
};

Second way:

$.fn.myplugin = function () {
    return this.bind('click', function () {
        console.log('Hi2');
    });
};
  • 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-06-07T12:08:07+00:00Added an answer on June 7, 2026 at 12:08 pm

    Let’s reduce from one to the other:

    $.fn.myplugin = function () {
        return $(this).each(function() {
            return $(this).bind('click', function () {
                console.log('Hi');
            });
        });
    };
    

    When you start your function this is a jQuery object, so $(this) doesn’t buy you anything, it could easily become:

    $.fn.myplugin = function () {
        return this.each(function() {
            return $(this).bind('click', function () {
                console.log('Hi');
            });
        });
    };
    

    So you are saying “For each element in the jQuery object, create a jQuery object and bind an event to that object.”

    If you look in bind, it calls on, which performs some logic and finally does this line:

    return this.each( function() {
        jQuery.event.add( this, types, fn, data, selector );
    });
    

    That means it is going to apply the event to every element in that jQuery object, so you are actually saying:

    So you are saying “For each element in the jQuery object, create a jQuery object, and for every element in that jQuery object bind an event to that element.”

    You are now looping twice, once in a list of N elements and then N times in lists of 1 element. You can actually just all bind directly:

    $.fn.myplugin = function () {
        return this.bind('click', function () {
            console.log('Hi2');
        });
    };
    

    And that of course, is the final code.

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

Sidebar

Related Questions

The following ways of writing a javascript function are equivalent. Maybe the first one
Is one of these two ways to create and initialize an object preferable? MyClass
I am a bit confused in the following two ways of initialisations..... Way 1:
I'm figuring out that there's two ways of writing C++ in Eclipse: either download
What is the difference between these two ways of converting a string to System.Guid
Out of curiosity, is there a (language independent*) way to make these two generic
So here I am with this simple question Consider these two for cycles and
There are two ways to overload operators for a C++ class: Inside class class
There are two ways to write a polymorphic migration in Rails. Generally, I've done
I know there are two ways, [self.navigationController pushViewController:siteViewController animated:YES]; [self presentModalViewController:anotherViewController animated:YES]; However, those

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.