나는 Jamie Talbot's technique for OOP in jQuery을보고있다.jQuery OOP 기술 혼란
result = $(result);
var template = result[0];
if (template)
{
for (var i in template)
{
if (typeof (template[i]) == 'function')
{
result[i] = function()
{
this.each(function()
{
this[i]();
});
};
}
}
}
나는 if (typeof (template[i]) == 'function')
블록까지 이해하는 코드가 각 공용 방법을보고 있는지,이에 대한 논리를 이해하려고 할 때 나는 거의 그러나 나는 약간 길을 잃지있어, 대부분이 이해 그 플러그인에서 선언하지만, 그 후에 그것은 나를 boggles.
누군가가 이것을 통해 나를 단계 수행 할 수있는 기회가 있습니까?
다음은 Jamie의 원본 코드입니다.
(function($) {
$.fn.encapsulatedPlugin = function(plugin, definition, objects, options) {
var result = [];
objects.each(function() {
var element = $(this);
if (!element.data(plugin)) {
// Initialise
var instance = new definition(this, options);
// Store the new functions in a validation data object.
element.data(plugin, instance);
}
result.push(element.data(plugin));
});
// We now have a set of plugin instances.
result = $(result);
// Take the public functions from the definition and make them available across the set.
var template = result[0];
if (template) {
for (var i in template) {
if (typeof (template[i]) == 'function') {
result[i] = function() {
this.each(function() {
this[i]();
});
};
}
}
}
// Finally mix-in a convenient reference back to the objects, to allow for chaining.
result.$ = objects;
return result;
};
})(jQuery);
majelbstoat의 답변 - 작업 코드 및 적절한 링크로 질문을 업데이트하는 것이 좋습니다. 건배. –