JavaScript: using the same name for class / function and object?

I looked here and basically (as far as I can tell) you cannot use the same name for a function and an object, but looking at the following code, this does not seem to be the case. Can someone tell me how this works?

;(function($){ $.fn.superfish = function(op){ var sf = $.fn.superfish, c = sf.c, $arrow = $(['<span class="',c.arrowClass,'"> &#187;</span>'].join('')), ... }; var sf = $.fn.superfish; ... sf.c = { bcClass : 'sf-breadcrumb', menuClass : 'sf-js-enabled', anchorClass : 'sf-with-ul', arrowClass : 'sf-sub-indicator', shadowClass : 'sf-shadow' }; ... })(jQuery); 

And the superfish has a link to itself in its declaration. Could this lead to infinite recursion?

+4
source share
2 answers

This is a general method that allows you to store a reference to some deeply nested property and use it instead for readability and performance. Crockford article related.

 // some really deeply nested property var o = ooo.eee.oo.ah_ah.ting.tang.walla.walla; // i could type... ooo.eee.oo.ah_ah.ting.tang.walla.walla.bing = true; // or just o.bing = true; 

It just happens that in this case the deeply nested property is the object itself, but javascript does not care.

This script demonstrates the exact javascript function you encountered. This is just the way javascript works. Not that I have included this feature as the basis of the paradigm, but it is possible.

+2
source

This is not recursion because it does not invoke itself. It refers to the properties of an object.

If you saw something like this:

 var sf = $.fn.superfish(), 

what will be the problem. :)

+4
source

All Articles