简体   繁体   English

如何从传递给实例调用的“ bind”方法的预定义函数中访问对象实例的上下文?

[英]How can I access an object instance's context from within a predefined function passed to the “bind” method invoked on the instance?

I have this snippet: 我有这个片段:

var test_img = $("<img />");
test_img.attr("src", img_url);
test_img.unbind("load");

console.log(test_img);

test_img.bind("load", function ()
    {
        console.log(this);
    });

The first console.log outputs: 第一个console.log输出:

[img]
    0: img  // expandable
    length: 1
    __proto__: Object[0]  // expandable

The second console.log outputs: 第二个console.log输出:

<img src=​"/​images/​Tree.jpg">​

Why are the two outputs different? 为什么两个输出不同?

Also, I need to define the function that gets passed to test_img.bind elsewhere. 另外,我需要定义在其他地方传递给test_img.bind的函数。 Ie, 也就是说,

function predefined_function()
{
    console.log(new_this);
}

var test_img = $("<img />");
test_img.attr("src", img_url);
test_img.unbind("load");

console.log(test_img);

test_img.bind("load", predefined_function);

How can I make it so that new_this is the same as this in the original snippet? 我怎样才能让这个new_this相同this在原来的片段?

As an answer to your first question, the difference is because the constructor of the first output is jQuery.fn.init , but the constructor of the second output is HTMLImageElement . 作为第一个问题的答案,不同之处在于,第一个输出的构造函数是jQuery.fn.init ,而第二个输出的构造函数是HTMLImageElement You can access image from first output using test_img[0] . 您可以使用test_img[0]从第一个输出访问图像。
If you want to use some predefined function, just replace new_this with this . 如果要使用一些预定义的函数,只需将new_this替换为this

See this example: 请参阅以下示例:

https://jsfiddle.net/y89zk2k8/1/ https://jsfiddle.net/y89zk2k8/1/

var textDiv = $("<div />").append("click me :)"),
    func = function() {
        var $me = $(this);
    console.log($me);
    };

textDiv.off("click");

console.log(textDiv);

textDiv.on("click", func);

$("body").append(textDiv);
  1. To make new_this same as this: You need to change it to the Jquery Oject in the event response function. 要使new_this与此相同:您需要在事件响应函数中将其更改为Jquery Oject。

  2. Define the function that gets passed to the event elsewhere: Pass the function as param to the event response function. 在其他地方定义传递给事件的函数:将函数作为参数传递给事件响应函数。

  3. Use .on to replace .bind, because .bind may be removed from future versions at any time. 使用.on替换.bind,因为.bind可能会随时从将来的版本中删除。 There is no reason to keep using .bind and every reason to prefer .on instead. 没有理由继续使用.bind,也没有理由更倾向于使用.on。

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

相关问题 如何访问本机对象调用的函数中的实例? - How to access instance in a function invoked by a native object? 如果 object 方法作为参数传递给 function,如何将 object 方法绑定到其上下文 - How do I bind an object method to its context if the object method is passed to a function as an argument 我可以从对象的方法中确定实例变量名称吗? - Can I determine the instance variable name from within an object's method? 当方法传递给另一个函数时,如何在实例方法中访问实例属性? - How to access instance property in an instance method when the method is being passed into an another function? 如何从匿名函数访问对象实例 - How to access object instance from anonymous function 如何将多个JavaScript对象实例绑定到元素 - How can i bind multiple javascript object instance to elements 如何从子实例访问 class 的实例? - How can I access an instance of a class from a child instance? 覆盖函数对象或访问实例方法中的方法,该方法传递给angularjs中的指令 - override a method in function object or access instance methods which is passed to a directive in angularjs 从同一对象的不同实例中触发另一个实例的功能 - Trigger another instance's function from within a different instance of same object 为什么我不能从Object实例访问Object方法? - Why I can't access Object methods from Object instance?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM