Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of a = a;? Analysing the jQuery source

From the jQuery 1.7rc1 source code (lines 1822-24):

if ( jQuery.isArray( name ) ) {
        name = name;
}

What's the point of having name = name? Could the two names be different?

like image 398
Randomblue Avatar asked Dec 05 '25 10:12

Randomblue


2 Answers

I think it is added for readability. The name should be an array, which it isn't always. In some cases it it converted to an array, but in this case, it is good 'as is'.

The whole fragment:

            // Support space separated names
            if ( jQuery.isArray( name ) ) {
                name = name;
            } else if ( name in thisCache ) {
                name = [ name ];
            } else {

                // split the camel cased version by spaces
                name = jQuery.camelCase( name );
                if ( name in thisCache ) {
                    name = [ name ];
                } else {
                    name = name.split( " " );
                }
            }
like image 72
GolezTrol Avatar answered Dec 08 '25 00:12

GolezTrol


The statement is probably added to be consistent with the other statements. The name = name can be omitted, but it's probably kept to clearly show the function of the block.

There is no way that a variable with exactely the same name hold different values, next to each other.

Code

// Support space separated names
if ( jQuery.isArray( name ) ) {
   name = name;
} else if ( name in thisCache ) {
    name = [ name ];
} else {
    // split the camel cased version by spaces
   name = jQuery.camelCase( name );
   if ( name in thisCache ) {
       name = [ name ];
   } else {
       name = name.split( " " );
   }
}
like image 31
Rob W Avatar answered Dec 08 '25 00:12

Rob W



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!