Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question on jquery function

 
Praful Thakare
Ranch Hand
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Liu Zhixiang
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
Praful Thakare
Ranch Hand
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64845
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not. Why would you use the new operator?
 
Praful Thakare
Ranch Hand
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64845
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.o00 you are so cool!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64845
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>>>>>>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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic