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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:53:48+00:00 2026-05-23T08:53:48+00:00

Because jQuery is a widely used and mature collaborative effort, I can’t help but

  • 0

Because jQuery is a widely used and mature collaborative effort, I can’t help but to look at its source for guidance in writing better Javascript. I use the jQuery library all the time along with my PHP applications, but when I look under the hood of this rather sophisticated library I realize just how much I still don’t understand about Javascript. Lo, I have a few questions for the SO community. First of all, consider the following code…

$('#element').attr('alt', 'Ivan is SUPER hungry! lolz');

vs

$('#element').attr({'alt': 'Ivan is an ugly monster! omfgz'});

Now, is this to say that the attr() method was designed to accept EITHER an attribute name, an attribute name and a value, or a pair-value map? Can someone give me a short explanation of what a map actually is and the important ways that it differs from an array in Javascript?

Moving on, the whole library is wrapped in this business…

(function(window, undefined) { /* jQuery */ })(window);

I get that the wrapped parentheses cause a behavior similar to body onLoad="function();", but what is this practice called and is it any different than using the onLoad event handler? Also, I can’t make heads or tails of the (window) bit there at the end. What exactly is happening with the window object here?

Am I wrong in the assessment that objects are no different than functions in Javascript? Please correct me if I’m wrong on this but $() is the all encompassing jQuery object, but it looks just like a method. Here’s another quick question with a code example…

$('#element').attr('alt', 'Adopt a Phantom Cougar from Your Local ASPCA');

… Should look something like this on the inside (maybe I’m wrong about this)…

function $(var element = null) {
    if (element != null) {
        function attr(var attribute = null, var value = null) {
            /* stuff that does things */
        }
    }
}

Is this the standing procedure for defining objects and their child methods and properties in Javascript? Comparing Javascript to PHP, do you use a period . the same way you would use -> to retrieve a method from an object?

I apologize for this being a bit lengthy, but answers to these questions will reveal a great deal to me about jQuery and Javascript in general. 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-23T08:53:49+00:00Added an answer on May 23, 2026 at 8:53 am

    1. Method overloading

    $('#element').attr('alt', 'Ivan is SUPER hungry! lolz');

    vs

    $('#element').attr({'alt': 'Ivan is an ugly monster! omfgz'});

    var attr = function (key, value) {
      // is first argument an object / map ?
      if (typeof key === "object") {
        // for each key value pair
        for (var k in key) {
          // recursively call it.
          attr(k, key[k]);
        }
      } else {
        // do magic with key and value
      }
    }
    

    2. Closures

    (function(window, undefined) { /* jQuery */ })(window);

    Is not used as an onload handler. It’s simply creating new scope inside a function.

    This means that var foo is a local variable rather then a global one. It’s also creating a real undefined variable to use since Parameters that are not specified passes in undefined

    This gaurds againts window.undefined = true which is valid / allowed.

    the (window) bit there at the end. What exactly is happening with the window object here?

    It’s micro optimising window access by making it local. Local variable access is about 0.01% faster then global variable access

    Am I wrong in the assessment that objects are no different than functions in Javascript?

    Yes and no. All functions are objects. $() just returns a new jQuery object because internally it calls return new jQuery.fn.init();

    3. Your snippet

    function $(var element = null) {

    Javascript does not support default parameter values or optional parameters. Standard practice to emulate this is as follows

    function f(o) {
      o != null || (o = "default");
    }
    

    Comparing Javascript to PHP, do you use a period . the same way you would use -> to retrieve a method from an object?

    You can access properties on an object using foo.property or foo["property"] a property can be any type including functions / methods.

    4. Miscellanous Questions hidden in your question

    Can someone give me a short explanation of what a map actually is and the important ways that it differs from an array in Javascript?

    An array is created using var a = [] it simply contains a list of key value pairs where all the keys are positive numbers. It also has all the Array methods. Arrays are also objects.

    A map is just an object. An object is simply a bag of key value pairs. You assign some data under a key on the object. This data can be of any type.

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

Sidebar

Related Questions

I have a partial view which gets rendered within a jQuery UI dialog. Because
I'm looking Jquery UI's modal form demo . But i want to add something.
This may be an open and close case but may open some debate. I
i have a problem with running my jQuery scripts in Internet Explorer at my
The jQuery validation plugin breaks for elements that have a HTML5 type defined. $(document).ready(function(){
I'm using the imagesize gem to check the sizes of remote images and then
I have a form that contains some Checkboxes and one Radio. The problem is
I have a list of input elements and using one selector I want to
I am trying to find the index of an object in an array in
I have the object var dataformdata={key1:value1,key2:value2}; then I add some more values with the

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.