Advantages of jQuery widget over regular JS object?

Edit: just to be clear, I will not doubt the jQuery awesomeness. I use it everywhere. I just don’t understand the benefits of using Widgets over regular js objects that use jQuery functionality.

I recently had to use some jQuery widgets in an application I'm working on, and I find them very unintuitive / ugly. There seem to be a lot of conventions to learn that don't really add any value, and the syntax does not take into account intellisense or simple refactoring.

Don't get me wrong, I use jQuery all the time and I hate life without it, just not seeing the value in the widget convention. I am sure that I do not have enough sense, because they are so common, so please tell me. Why do people create jQuery widgets instead of regular js objects?

Simplified procedure example:

Regular Javascript Object:   

//instantiate & render
var foo = new MyDomain.MyObject('#mySpecialInput', options)
foo.render();

//assign to property
foo.value = 5;
//call method
foo.multiplyBy(2);
//bind event
foo.change(function(){
    alert('it changed');
}

JQuery Widget:   

//instantiate & render
var $foo = $('#mySpecialInput');
$foo.myWidget(options);

//assign to property
$foo.myWidget('option', 'value', 5);
//call method
$foo.myWidget('multiplyBy', 2);
//bind event
$foo.bind('myWidgetChange', function(){
    alert('it changed');
}
+5
source share
3 answers

, jQuery , . , , .bind(), . Googling jQuery, .

() , , jQuery.


, , jQuery :

... , . ( " , , , " ) ( " expando" ). , "expando ", . $(document).ready().

+1

, jQuery , OO ( , js)

-
- - .
- , , , jQuery
- : ,

jQuery OO.

API. , jQuery, , - , . - http://wiki.jqueryui.com/w/page/12138135/Widget%20factory

+1
  • Organization and structuring of code
  • Namespaces
  • Inheritance
  • Proper scaling and closing
  • readability and overall cross-developer architecture
  • predefined properties for accessing shared elements
0
source

All Articles