【发布时间】:2014-10-12 19:37:32
【问题描述】:
通常在编写 jQuery 时我只使用函数。这次我想给它一点最佳实践,所以我遵循了一个教程。 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);
在 start 和 attach 函数中,我可以通过在其前面放置“this”来调用每个函数。但是当我尝试从 openLink() 和 triggerDialog() 调用 openDialog() 时,我得到 - Uncaught TypeError: undefined is not a function。
为什么会这样,我应该怎么做才能解决它?
【问题讨论】:
标签: javascript jquery scope undefined