Вопрос

I try to see what is the logic behind a jQuery plugin, and I am stacked with the following issue.

I have the following code, and I cannot determine what should be the value of the var f inside the variable t = function(u, a){}

(
    function($)
    {                            
        $.fn.pluginName = function(userOptions)
        {

            ...

            if((typeof userOptions).match("object|undefined"))
            {
                return this.each(
                    function(e)
                    {
                        new t(this, userOptions)
                    }
                );
            }

            var t = function (u, a)
            {
                var f = this;
            }

            ...

        }
    }
)(jQuery)

It should contain a reference in function t or it should has a reference on pluginName

Can somebody to help me with that ? I am totaly confused with that.

Это было полезно?

Решение

It appears that function t is a constructor function, which means that this inside the function will refer to the instance that is created when that function is invoked with the new keyword.

Therefore f will refer to the instance of the object whose constructor is function t.

Другие советы

When I want to find out what this is I simply print it with console.log(this); to increase readability you can add toStrings (to the object or its prototype).

In addition I think GregL has it right in this example. But it's a strange example you have provided when trying to figure out this. Some names to the constructor function and actually saving the object created using new in a variable would help GregLs explaination make sense

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top