Javascript模块模式:在嵌套函数中调用函数
我有这样的代码:Javascript模块模式:在嵌套函数中调用函数,javascript,jquery,function,Javascript,Jquery,Function,我有这样的代码: var SEVERINU = SEVERINU || {}; SEVERINU.AutoGallery = { galleryImagesNames: new Array(), findImages: function () { var dirToResearch = this.galleryName; $.post( "_php/get-files.php", { dir:
var SEVERINU = SEVERINU || {};
SEVERINU.AutoGallery = {
galleryImagesNames: new Array(),
findImages: function () {
var dirToResearch = this.galleryName;
$.post(
"_php/get-files.php", {
dir: dirToResearch,
ext: this.fileExtension
}, function (data) {
var pliki = $.parseJSON(data);
console.log(data);
for (var i in pliki) {
this.galleryImagesNames.push(pliki[i]); // problem !
}
});
},
}
此行:This.gallerymagesnames.push(pliki[i])代码>给我带来了问题
上面说他找不到galleryImagesNames等
如果我要“deep”,如何调用函数var?只需将当前此
保存到其他变量,这样它就不会在函数内部被覆盖
var SEVERINU = SEVERINU || {};
SEVERINU.AutoGallery = {
galleryImagesNames : new Array(),
findImages : function(){
var self = this; // keep current value of this in variable named self
var dirToResearch = this.galleryName;
$.post(
"_php/get-files.php",
{
dir: dirToResearch,
ext: this.fileExtension
},
function(data){
var pliki = $.parseJSON(data);
console.log(data);
for(var i in pliki){
// call the method using self variable instead of this that got overwritten
self.galleryImagesNames.push(pliki[i]);
}
}
);
},
}
只需将当前此
保存到其他变量,这样它就不会在函数中被覆盖
var SEVERINU = SEVERINU || {};
SEVERINU.AutoGallery = {
galleryImagesNames : new Array(),
findImages : function(){
var self = this; // keep current value of this in variable named self
var dirToResearch = this.galleryName;
$.post(
"_php/get-files.php",
{
dir: dirToResearch,
ext: this.fileExtension
},
function(data){
var pliki = $.parseJSON(data);
console.log(data);
for(var i in pliki){
// call the method using self variable instead of this that got overwritten
self.galleryImagesNames.push(pliki[i]);
}
}
);
},
}
实际上,这是因为您的$.post
的作用域与对象的其余部分不同。试试这个:
findImages : function(){
var dirToResearch = this.galleryName;
var gImageNames = this.galleryImagesNames;
$.post(
"_php/get-files.php",
{
dir: dirToResearch,
ext: this.fileExtension
},
function(data){
var pliki = $.parseJSON(data);
console.log(data);
for(var i in pliki){
gImageNames.push(pliki[i]); // we have access to this variable
}
}
);
},
实际上,这是因为您的$.post
的作用域与对象的其余部分不同。试试这个:
findImages : function(){
var dirToResearch = this.galleryName;
var gImageNames = this.galleryImagesNames;
$.post(
"_php/get-files.php",
{
dir: dirToResearch,
ext: this.fileExtension
},
function(data){
var pliki = $.parseJSON(data);
console.log(data);
for(var i in pliki){
gImageNames.push(pliki[i]); // we have access to this variable
}
}
);
},
由于您在匿名函数中,因此必须再次引用this
关键字。或者像这样直接使用它:
SEVERINU.AutoGallery.galleryImagesNames.push(pliki[i]);
通常,当您多次需要引用SEVERINU.AutoGallery
时,最好将其存储在如下变量中:
var that = SEVERINU.AutoGallery; // or "self" or something you prefer
for (var i in pliki) {
that.galleryImagesNames.push(pliki[i]);
}
这是因为javascript在访问对象名称空间时速度较慢。你走得越深,速度就越慢。引用的次数越多,它会变得越慢,因为必须首先解析函数作用域才能访问下一个/上一个作用域。由于您在匿名函数中,因此必须再次引用此
关键字。或者像这样直接使用它:
SEVERINU.AutoGallery.galleryImagesNames.push(pliki[i]);
通常,当您多次需要引用SEVERINU.AutoGallery
时,最好将其存储在如下变量中:
var that = SEVERINU.AutoGallery; // or "self" or something you prefer
for (var i in pliki) {
that.galleryImagesNames.push(pliki[i]);
}
这是因为javascript在访问对象名称空间时速度较慢。你走得越深,速度就越慢。引用越多,它将变得越慢,因为必须首先解析函数作用域才能访问下一个/上一个作用域。您也可以在上下文中使用传递
var SEVERINU = SEVERINU || {};
SEVERINU.AutoGallery = {
galleryImagesNames : new Array(),
findImages : function(){
var dirToResearch = this.galleryName;
$.post(
"_php/get-files.php",
{
dir: dirToResearch,
ext: this.fileExtension
},
$.proxy(function(data){ //Set up for the context to be passes for this function.
var pliki = $.parseJSON(data);
console.log(data);
for(var i in pliki){
this.galleryImagesNames.push(pliki[i]); // problem solved. now this doesn't refer to ajax context
}
},this) <-- Pass the context here
);
},
}
var SEVERINU=SEVERINU | |{};
SEVERINU.AutoGallery={
galleryImagesNames:新数组(),
findImages:function(){
var dirToResearch=this.galleryName;
美元邮政(
“_php/get files.php”,
{
dir:dirToResearch,
ext:this.fileExtension
},
$.proxy(函数(数据){//设置要为此函数传递的上下文。
var pliki=$.parseJSON(数据);
控制台日志(数据);
用于(pliki中的var i){
this.gallerymagesnames.push(pliki[i]);//问题已解决。现在,这不涉及ajax上下文
}
},这)您也可以在上下文中使用传递
var SEVERINU = SEVERINU || {};
SEVERINU.AutoGallery = {
galleryImagesNames : new Array(),
findImages : function(){
var dirToResearch = this.galleryName;
$.post(
"_php/get-files.php",
{
dir: dirToResearch,
ext: this.fileExtension
},
$.proxy(function(data){ //Set up for the context to be passes for this function.
var pliki = $.parseJSON(data);
console.log(data);
for(var i in pliki){
this.galleryImagesNames.push(pliki[i]); // problem solved. now this doesn't refer to ajax context
}
},this) <-- Pass the context here
);
},
}
var SEVERINU=SEVERINU | |{};
SEVERINU.AutoGallery={
galleryImagesNames:新数组(),
findImages:function(){
var dirToResearch=this.galleryName;
美元邮政(
“_php/get files.php”,
{
dir:dirToResearch,
ext:this.fileExtension
},
$.proxy(函数(数据){//设置要为此函数传递的上下文。
var pliki=$.parseJSON(数据);
控制台日志(数据);
用于(pliki中的var i){
this.gallerymagesnames.push(pliki[i]);//问题已解决。现在,这不涉及ajax上下文
}
},这)上下文(这
)已更改,这
不再是您认为在回调中的内容。那么如何调用galleryImagesNames?请查看任何现有答案。上下文(这
)已更改,此
不再是回调中您认为的内容。那么如何调用galleryImagesNames?查看任何现有答案。此答案对我有用,但希望使用此选项,以防有人将SEVERINU.Autogallery部分更改为其他内容。特别感谢您在for循环中访问SEVERINU.AutoGallery
是对象长度的倍。当存储在变量中时,它将被引用,性能更高。例如,jquery选择器也是如此。这一个对我有用,但我想使用它,以防有人将SEVERINU.Autogallery的一部分更改为其他内容。特别感谢您将访问的for循环SEVERINU.AutoGallery
是对象长度的两倍。当存储在变量中时,它将被引用,并且性能更高。例如,jquery选择器也是如此。它返回空数组。数组在$.post内之前看起来很好。在…之后,它是完全空的。您能告诉我为什么吗?它返回空数组。数组看起来很好od直到$post内,之后…它是完全空的。你能告诉我为什么吗?