[英]this works on some functions and not others
Normally when writing jQuery i just use functions. 通常,在编写jQuery时,我只是使用函数。 This time I want to give it a little sprinkle of best practice and so I followed a tutorial. 这次我想给它一些最佳实践,所以我遵循了一个教程。 The javascript itself seems to be correct but I am having a few problems calling certain functions. javascript本身似乎是正确的,但是调用某些函数时遇到一些问题。
jQuery.noConflict();
(function($j) {
'use strict';
function Site(settings) {
this.windowLoaded = false;
}
Site.prototype = {
constructor: Site,
start: function() {
var me = this;
$j(window).load(function() {
me.windowLoaded = true;
});
this.attach();
},
attach: function() {
this.getPrimaLink();
this.onCloseDialog();
this.triggerDialog();
this.openLink();
},
getPrimaLink: function(){
if($j('#order-data').is(":visible")){
$j( ".content-header" ).append($j( "#findPrimaLink" ));
$j('#findPrimaLink').show();
}
},
onCloseDialog: function(){
$j('#dialog').bind('dialogclose', function(event) {
$j( ".content-header" ).append($j( "#findPrimaLink" ));
$j('#findPrimaLink').show();
});
},
triggerDialog: function(){
$j("[title='Create New Customer']").click(function(){
$j('#findPrimaLink').show();
>>>>> this.openDialog(); <<<<<<
})
},
openLink: function(){
$j('#findPrimaLink').click(function(){
>>> this.openDialog(); <<<<<
});
},
openDialog: function(){
$j( "#dialog" ).dialog({
height: 'auto',
width: 350,
modal: true,
resizable:false,
});
},
};
$j(document).ready(function($j) {
var site = new Site();
site.start();
});
})(jQuery);
Within the start and attach function I am able to call each function by placing 'this' in front of it. 在start和attach函数中,我可以通过在其前面放置“ this”来调用每个函数。 But when I try to call openDialog() from openLink() and triggerDialog() I get - Uncaught TypeError: undefined is not a function. 但是,当我尝试从openLink()和triggerDialog()调用openDialog()时,我得到了-未被捕获的TypeError:undefined不是一个函数。
Why is this and what should I do to fix it? 为什么会这样,我应该怎么做才能解决?
For both functions you're having a problem with, you're trying to use this
inside of a jQuery function, so this
's scope is to the DOM element, not the Site
class . 对于您遇到的两个函数,您都试图在jQuery函数中使用this
,因此this
的作用域是DOM元素,而不是Site
类 。
triggerDialog: function(){
var site = this;
$j("[title='Create New Customer']").click(function(){
$j('#findPrimaLink').show();
site.openDialog();
console.log(this); //remove this for production, but you can see that `this` points to a DOM element
})
},
openLink: function(){
var site = this;
$j('#findPrimaLink').click(function(){
site.openDialog();
});
},
To understand why this happens, you should read about javascript Closures. 要了解发生这种情况的原因,您应该阅读有关javascript闭包的信息。 Here and here . 在这里和这里 。
PS you have an extra comma after your openDialog
function. PS,您的openDialog
函数后还有一个逗号。
PPS It's also worth noting that this is exactly what're you're doing inside the start
method. PPS还值得注意的是,这正是您在start
方法中正在执行的操作。
var me = this;
$j(window).load(function() {
me.windowLoaded = true;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.