admin管理员组文章数量:1405147
Let's say that we have a javascript object called aObject and the test() function is used as a callback function in JQuery
var aObject = {
aVariable : 'whatever value',
test : function() {
// Trying to access property. But doesn't work as expected since I am getting the DOM element, not the aObject reference
var temp = this.aVariable;
}
}
var anInstanceOfAObject = $.extend({}, aObject);
anInstanceOfAObject.someFunction = function () {
// I have to put "this" in a variable since "this" in the context below refers to the DOM element, not the instance of the object
var placeHolder = this;
$('some random div.element').theJavascriptFunction({
"theJavascriptCallbackFunction": placeHolder.test,
});
}
Inside that test() function, normally the context of "this" is the DOM element. My question is how to reference aObject since we can't use "this" to reference it.
EDIT: I am not sure if the syntax above is the correct/preferred way to instantiate an Object. I see some examples using this syntax
var aObject = function() {....
Please inform me if this seems to be relevant to the problem.
Let's say that we have a javascript object called aObject and the test() function is used as a callback function in JQuery
var aObject = {
aVariable : 'whatever value',
test : function() {
// Trying to access property. But doesn't work as expected since I am getting the DOM element, not the aObject reference
var temp = this.aVariable;
}
}
var anInstanceOfAObject = $.extend({}, aObject);
anInstanceOfAObject.someFunction = function () {
// I have to put "this" in a variable since "this" in the context below refers to the DOM element, not the instance of the object
var placeHolder = this;
$('some random div.element').theJavascriptFunction({
"theJavascriptCallbackFunction": placeHolder.test,
});
}
Inside that test() function, normally the context of "this" is the DOM element. My question is how to reference aObject since we can't use "this" to reference it.
EDIT: I am not sure if the syntax above is the correct/preferred way to instantiate an Object. I see some examples using this syntax
var aObject = function() {....
Please inform me if this seems to be relevant to the problem.
Share Improve this question asked Sep 5, 2011 at 6:16 developarvindeveloparvin 5,05912 gold badges57 silver badges101 bronze badges 2- by this you mean aObject right? – Baz1nga Commented Sep 5, 2011 at 6:19
- yes. I want to get aObject, not the DOM element(much better if there is a way to get both). – developarvin Commented Sep 5, 2011 at 6:22
3 Answers
Reset to default 1You just need to wrap your method call to get the right this
:
anInstanceOfAObject.someFunction = function () {
var placeHolder = this;
$('some random div.element').theJavascriptFunction({
"theJavascriptCallbackFunction": function() { placeHolder.test() }
});
}
When you use just placeHolder.test
as the callback, you're just handing over a reference to the test
function and that function will be called with the DOM element as this
.
You could also try bind
:
anInstanceOfAObject.someFunction = function () {
var placeHolder = this;
$('some random div.element').theJavascriptFunction({
"theJavascriptCallbackFunction": this.test.bind(this)
});
}
If you wrap a jquery function call with $.proxy(function, this) then jquery will fix your reference to this so it works the way you want.
First, your question is right on. However your code doesn't work and when its fixed it illustrates a solution to the problem. A short lesson: you will learn more if you debug your problem code first!
Below I will supply the problem, the solution you illustrate and a more elegant solution.
Here is the object in question:
var aObject = {
aVariable : 'whatever value',
test : function() {
// Trying to access property.
//But doesn't work as expected since I am getting
//the DOM element, not the aObject reference
var temp = this.aVariable;
alert("temp=" + temp);
}
}
Here is an example of the problem:
var anInstanceOfAObject = $.extend({}, aObject);
anInstanceOfAObject.someFunction = function() {
$(function() {
//The problem. 'this' is not set after calling the fn via jquery.
this.test();
});
anInstanceOfAObject.someFunction();
Here is the solution you coded:
var anInstanceOfAObject = $.extend({}, aObject);
anInstanceOfAObject.someFunction = function() {
// by saving this in placeHolder you solve the problem. Good!
var placeHolder = this;
$(function() {
// Your solution works. Here you pass forward your ref to this
placeHolder.test();
});
}
anInstanceOfAObject.someFunction();
Finally here is a slightly more elegant answer:
var anInstanceOfAObject = $.extend({}, aObject);
anInstanceOfAObject.someFunction = function() {
$(
$.proxy(function(){
// using $.proxy gets jquery to fix your this ref
this.test();
},this)
);
}
anInstanceOfAObject.someFunction();
this always refers to the dom element. to get the jQuery object related to the element you need to wrap it in the jquery again, so either $(this) or jQuery(this) depending on your setup.
本文标签: javascriptHow to get the object reference inside a JQuery callback functionStack Overflow
版权声明:本文标题:javascript - How to get the object reference inside a JQuery callback function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744869295a2629532.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论