4

I've looked here and basically (as far as I can tell) you can't 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 anyone 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 superfish has a reference to itself within its declaration. Would this not cause infinite recursion?

2
  • Superfish decelerates? Preposterous. Commented Jul 13, 2012 at 18:31
  • Your right, nothing can slow superfish down! Commented Jul 13, 2012 at 18:58

2 Answers 2

4

It is not recursion since it is not calling itself. It is referencing the properties off of the object.

If you saw something like this:

var sf = $.fn.superfish(),

than there would be an issue. :)

Sign up to request clarification or add additional context in comments.

2 Comments

Its just in my mind, sf references superfish(). sf defines properties that superfish() uses. How can it reference superfish() before defining the properties that superfish() needs? Does the act of 'var sf = $.fn.superfish;' create a "blank" $.fn.superfish object?
it is called after the properties are assigned to the object.
2

This is a common technique that allows you to store a reference to some deeply nested property and use that instead, for readability and performance. Crockford's article is 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 fiddle demonstrates the exact javascript feature you're having trouble with. That's just the way javascript works. Not that I'd embrace this feature as a paradigm foundation, but it's possible.

4 Comments

Though I'm still trying to understand how a reference can be made to the object even though the object itself requires the properties defined using the sf reference. Its like "what came first, the chicken of the egg?". Or am I just totally misinterpreting it?
memory for the object is set aside once it's referenced. This leaves place holders. Those place holders are then filled in after. If you tried using the object before they were defined you'd see "undefined" where the empty variables are.
Thanks. I guess I'll just have to accept the fact that nested properties can be defined through a reference to an object that isn't defined yet. I think I'm going to go cry in a corner for a bit.
@Nate added a jsfiddle example for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.