Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
// Go through the array, translating each of the items to their
if ( isArray ) {
for ( ; i < length; i++ ) {
value = callback( elems[ i ], i, arg );
if ( value != null ) {
ret[ ret.length ] = value;
}
}
// Go through every key on the object,
} else {
for ( key in elems ) {
value = callback( elems[ key ], key, arg );
if ( value != null ) {
ret[ ret.length ] = value;
}
}
}
If using jQuery 1.6, the code if ( $(elem).attr(«checked») ) will retrieve the actual content attribute, which does not change as the checkbox is checked and unchecked. It is meant only to store the default or initial value of the checked property. To maintain backwards compatability, the .attr() method in jQuery 1.6.1+ will retrieve and update the property for you so no code for boolean attributes is required to be changed to .prop(). Nevertheless, the preferred way to retrieve a checked value is with one of the options listed above. To see how this works in the latest jQuery, check/uncheck the checkbox in the example below.
можно работать «уже около двух лет» и не испытывать потребности в привязке контекста
var that = this;
$('a').click(function(){
that.doStuff();
});
$.proxy()?function_name({
method: function() {
var that = this;
$('a').click(function(){
that.doStuff();
});
}
})
var that = this; не поможет. function_name({
method: function() {
$('a').click($.proxy(this.doStuff, this));
}
})
jQuery.grep(array, fn) => array.filter(fn)
jQuery.map(array, fn) => array.map(fn)
jQuery.proxy(fn, context) => fn.bind(context)
Опять же underscore не такой тяжёлый, поэтому весь jQuery сильно не вырастетиз-за него.
Но вообще полная синхронизация необязательна, просто надо добавить сразу разумный набор функций, которые настолько базовые, что в принципе от версии к версии практически не меняются.
jQuery.proxy() может использоваться так: jQuery.proxy(obj, fn), где obj — объект, а fn — название метода этого объекта.var arr= [1,2,3]
, join= jQuery.proxy(arr, 'join')
;
join(', ') // => 1, 2, 3
jQuery.proxy(obj.fn, obj).var pfn = $.proxy(context, 'method'); // или вариант из основного текста статьи
pfn(a, b); // => context.method(a, b);
var pfn = $.proxy(context, 'method', a, b); // или вариант первых двух аргументов из основного текста статьи
pfn(c); // => context.method(a, b, c);
queue согласен — полезный метод.$.fn.self — вроде нету такого. Может имелось в виду .andSelf()?
5 полезных методов jQuery API, о которых вы могли не знать