aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes question on jquery function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "question on jquery function " Watch "question on jquery function " New topic
Author

question on jquery function

Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 641
Hi,

can some one tell me difference between following two declaration

1) $.fn.suggest = function(source, options) {...});
2) $.suggest = function(input, options) {...}

-P
[ August 19, 2008: Message edited by: Praful Thakare ]

All desirable things in life are either illegal, banned, expensive or married to someone else !!!
Liu Zhixiang
Ranch Hand

Joined: Aug 12, 2006
Posts: 32
1) $.fn.suggest = function(source, options) {...});

If you choice this.
You can
$('#foo').suggest();

2) $.suggest = function(input, options) {...}
But for this one.You can only using like
$.suggest();


I Think Therefore I Am
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 641
thanks much Liu ,so why is the later one called using new operator ?

new $.suggest(this, options);

-P
[ August 19, 2008: Message edited by: Praful Thakare ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61612
    
  67

It's not. Why would you use the new operator?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 641
well i see it one of the auto complete plug in called jquery.suggest.js_1.1_0

following is the code




and suggest is defined as



if i remove new then the code does not work.

-P
[ August 19, 2008: Message edited by: Praful Thakare ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61612
    
  67

Any function can be used as a constructor in JavaScript. That is however, not a typical use of such functions -- especially in jQuery -- and I would not use it as a pattern for your own code.

And it is most certainly not needed when calling functions defined using the $ namespace.
[ August 19, 2008: Message edited by: Bear Bibeault ]
Liu Zhixiang
Ranch Hand

Joined: Aug 12, 2006
Posts: 32
First the author defined a class.
He use $.className for controlling the namespace.
So for thie question,you should use new $.suggest(),but not $.suggest() directly.

And then,he write $.fn.functionName to let
new $.suggest(input ,options ) == $(input).suggest(options);
As the later one is more jquery.
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 641
hmmm..bit confused, but this is what i understood

1) any thing thats defined using $.somename(paramlist...) can be invoked using new operator but it should not be used.

2) anything thats defined as $.fn.somename(paramlist...) can be invoked using selector.somename(paramlist...)

if this is correct then i am cool.


-P
Liu Zhixiang
Ranch Hand

Joined: Aug 12, 2006
Posts: 32
.o00 you are so cool!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61612
    
  67

Just a point of clarification on (1). As I said earlier, any JavaScript function can be used and invoked as a constructor. Whether that makes sense or not depends upon the purpose for which the function was written. In general, functions not intended to be used as constructors shouldn't be used that way.

The use of constructors in jQuery and its plugins is not typical.
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 641
>>>>>>The use of constructors in jQuery and its plugins is not typical.

cool....my thumb rule ,anything that not typical don't used it

-P
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question on jquery function