简体   繁体   English

在jQuery中,在这种情况下如何正确使用“ this”?

[英]In jQuery, how do you use 'this' properly in this situation?

I am confused on why this code is written like this, but I am sure it is important to understand: 我对为什么这样编写代码感到困惑,但是我确信了解以下内容很重要:

$.fn.mapImage = function(options){
    //Set user input options and defaults
    var optionConfig = $.extend({}, $.fn.mapImage.defaults, options);

    image=this;
    this.image = this;

    // Assign defaults
    this.getUrl = optionConfig.getUrl;
    this.saveUrl = optionConfig.saveUrl;
    this.deleteUrl = optionConfig.deleteUrl;
    this.editable = optionConfig.editable;
    this.pinpoints = optionConfig.pinpoints;
    image.pinpoints = optionConfig.pinpoints;
    image.pinpointCount = 0;


    this.canvas = $('<div class="canvas"><div class="create-mode"></div><div class="edit-mode"></div></div>');
    this.image.after(this.canvas);
    this.canvas.height(this.height());
    this.canvas.width(this.width());
    this.canvas.css('background-image', 'url("' + this.attr('src') + '")');
    this.canvas.children('.create-mode').css('cursor', 'crosshair');
    this.canvas.children('.create-mode, .edit-mode').height(this.height());
    this.canvas.children('.create-mode, .edit-mode').width(this.width());
    this.canvas.children('.edit-mode').show();
    this.canvas.children('.create-mode').hide();
    $(this).hide();


}

What I dont understand is why does the code have image=this and this.image=this , is it the same thing? 我不明白的是,为什么代码中有image=thisthis.image=this ,这是同一回事吗? why not do something like image.after(this.canvas) does this this refer to the current object that is passed through the function in the first place? 为什么不做诸如image.after(this.canvas)类的image.after(this.canvas) this指向通过函数传递的当前对象?

The reason why this is confusing (no pun intended) is because you are missing a bit of code for the example above to make sense: 之所以会造成混淆(没有双关语),是因为您缺少使上面的示例有意义的代码:

var image; // necessary otherwise you'll get a runtime exception.

$.fn.mapImage = function(options){
    //Set user input options and defaults
    var optionConfig = $.extend({}, $.fn.mapImage.defaults, options);

    image=this;
    this.image = this
    ...

Now, when you look at: 现在,当您查看:

image=this;
this.image = this

It makes more sense doesn't it? 这更有意义吗? The first assignment is for the local variable ' var image '. 第一个分配是针对局部变量“ var image ”的。 That way, a reference is kept even when the function has finished executing. 这样,即使函数已完成执行,仍保留引用。

The second assignment is for a property of the current object, denominated by ' this '. 第二个分配是针对当前对象的属性,以“ this ”表示。

Here's your explanation, but to me there is no point of using both this.image and image in the code. 这是您的解释,但对我this.image ,在代码中同时使用this.imageimage毫无意义。 Just using image would be fine. 仅使用image就可以了。

What I dont understand is why does the code have image=this and this.image=this , is it the same thing? 我不明白的是,为什么代码中有image=thisthis.image=this ,这是同一回事吗?

no, they are not... 不,他们不是...

image = this;
this.image = this;

in that we can say that image and this.image are referring to same thing this , right? 因为我们可以说imagethis.image指的是同一件事this ,对吗?

image = this;
// ^      ^----- current object
// | --- this image is a variable
this.image = this;
//     ^--- this image is a property of current object, this 
// because of image = this;, we can also say that
image.image = this;
// ^    ^      ^----- current object
// |    |------------ property
// |----------------- variable

you can also get some idea here 你也可以在这里得到一些想法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM