gpt4 book ai didi

javascript - JS : why is the var not declared when it is declared inside a method of an object and passed as a parameter to the other method of the same object?

转载 作者:行者123 更新时间:2023-11-28 18:44:06 25 4
gpt4 key购买 nike

当变量在对象的方法内声明并作为参数传递给同一对象的其他方法时,我不知道为什么会收到错误“ReferenceError i is not returned”。

这是我的 JS:

var reservasAPP = {   
generarGrilla: function(){
//la llamo desde el html con jQuery(document).ready();
for(var i=1; i<13; i++){
var impresionGrilla = $('#grilla').append("<button class=\"btn btn-primary btn-xs\" onclick=\"return reservasAPP.guardarReserva(i);\">Reservar</button>");
};
},
nombre: function (i) {
return $('#horario'+i).val();
},
guardarReserva:function(i){
var reservaConfirmada = $('#horario'+i).html('--> '+this.nombre());
console.log("whatever "+i);
console.log(i);
return false;
},
}

window.reservas = reservasAPP;

如果我进入控制台并尝试输入“var i”来获取 var i 的值,我会得到“未定义”。为什么未定义?

最佳答案

试试这个...

var reservasAPP = {   
generarGrilla: function(){
//la llamo desde el html con jQuery(document).ready();
for(var i=1; i<13; i++){
var impresionGrilla = $('#grilla').append("<button class=\"btn btn-primary btn-xs\" onclick=\"return reservasAPP.guardarReserva(" + i + ");\">Reservar</button>");
};
},
nombre: function (i) {
return $('#horario'+i).val();
},
guardarReserva:function(i){
var reservaConfirmada = $('#horario'+i).html('--> '+this.nombre(i));
console.log("whatever "+i);
console.log(i);
return false;
},
}

说明 "<button class=\"btn btn-primary btn-xs\" onclick=\"return reservasAPP.guardarReserva(i);\">Reservar</button>"被注入(inject)到 DOM 中,但变量“i”在那里没有任何意义。

此外,“nombre”函数需要一个参数,该参数显然应该是传递给“guardarReserva”函数的参数。因此,以下行已更改:

var reservaConfirmada = $('#horario'+i).html('--> '+this.nombre());

var reservaConfirmada = $('#horario'+i).html('--> '+this.nombre(i));

关于javascript - JS : why is the var not declared when it is declared inside a method of an object and passed as a parameter to the other method of the same object?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35654523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com