下面jquery插件中代码引用中的this关键字是什么?
我在网上找到了这段代码,想知道每个“this”关键字引用的是什么?这段代码有7个“This”关键字的引用,不知道每个关键字引用了什么。。我假设它引用了某个DOM元素,但无法在脑海中建立连接下面jquery插件中代码引用中的this关键字是什么?,jquery,Jquery,我在网上找到了这段代码,想知道每个“this”关键字引用的是什么?这段代码有7个“This”关键字的引用,不知道每个关键字引用了什么。。我假设它引用了某个DOM元素,但无法在脑海中建立连接 (function($) { $.watermark = function(element, options) { // what does this keyword reference to this.options = {}; // what does thi
(function($) {
$.watermark = function(element, options) {
// what does this keyword reference to
this.options = {};
// what does this keyword reference to
element.data('watermark', this);
// what does this keyword reference to
this.init = function(element, options) {
// what does this keyword reference to
this.options = $.extend({}, $.watermark.defaultOptions, options);
//Manipulate element here ...
};
//Public function
// what does this keyword reference to
this.greet = function(name) {
console.log('Hello, ' + name + ', welcome to Script Junkies!');
};
// what does this keyword reference to
this.init(element, options);
};
$.fn.watermark = function(options) { //Using only one method off of $.fn
return this.each(function() {
(new $.watermark($(this), options));
});
};
$.watermark.defaultOptions = {
class: 'watermark',
text: 'Enter Text Here'
}
})(jQuery);
它指的是该函数作为方法的对象
window我已将其标记为内联,如下所示:
(function($) {
$.watermark = function(element, options) {
// referencing the caller of plugin (which is acting like an object)
this.options = {}; // this.options is a property
element.data('watermark', this); //referencing the caller of the plugin
// referencing the caller plugin and init() is a function of the "object"
this.init = function(element, options) {
// referencing the caller of the plugin again, and it's options property
this.options = $.extend({}, $.watermark.defaultOptions, options);
//Manipulate element here ...
};
//Public function
//referencing the caller plugin again, which is an object
this.greet = function(name) {
console.log('Hello, ' + name + ', welcome to Script Junkies!');
};
// referencing the caller of the plugin again
this.init(element, options); //calling the init function of the object
};
$.fn.watermark = function(options) { //Using only one method off of $.fn
//referencing the caller of the plugin again
return this.each(function() {
// representing the current element in the iteration
(new $.watermark($(this), options));
});
};
$.watermark.defaultOptions = {
class: 'watermark',
text: 'Enter Text Here'
}
})(jQuery);
this=watermark对象。您还必须了解
如果通过call()
(function.prototype.call())element.data('watermark',this)调用函数,则可以设置此代码>:这不是引用元素。。。“如果这不是你的意思,这里有点不清楚你是对的,@karlandréGagnon。那是个错误。实际上我是先做的,单引号指的是调用方
。为了清晰起见,我后来更改了行话,但没有提到有没有办法找到插件的调用者?我标记为“引用插件调用者”的所有this
关键字都是插件的调用者。我不确定我是否理解你的要求?使用以下语法调用插件:var$caller=$(“someSelector”)$caller.somePlugin()他想知道每个人具体指的是什么。