Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

When to use $.fn.something = function() versus just function something()  RSS feed

 
ranger
Sheriff
Posts: 17314
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess the subject says it all.

I was creating a function to use in my js files to do popups alerts, and I first tried with $.fn.showAlert = function(message). But then changed it to function showAlert(message).

So when should you use one over the other? benefits?

Thanks

Mark
 
Author and ninkuma
Marshal
Posts: 66677
167
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
$.fn is the jQuery prototype. You would create such a function when you want to extend jQuery and call the function as a method of the wrapped set. Within the function, the function context (this) refers to the wrapped set.

If that's not what you want to do, simply create a standalone function.
 
Mark Spritzler
ranger
Sheriff
Posts: 17314
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:$.fn is the jQuery prototype. You would create such a function when you want to extend jQuery and call the function as a method of the wrapped set. Within the function, the function context (this) refers to the wrapped set.

If that's not what you want to do, simply create a standalone function.



Great perfect, what I was thinking. So like a showAlert which isn't a function to act on a wrapped set, it should be function showAlert().

Mark
 
That feels good. Thanks. Here's a tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!