我想在另一个函数total
中使用call
的值。
function recalculateTotal(sc) {
var total = 0;
//basically find every selected seat and sum its price
sc.find('selected').each(function() {
total += this.data().price;
});
return total;
}
function call() {
var a = recalculateTotal(sc);
window.location.replace("Demo2.jsp?name=" + a);
}
答案 0 :(得分:0)
这取决于变量sc的范围。 sc全球化吗?您可以执行以下操作:
var scInput = 'whatever';
var total;
function recalculateTotal(sc) {
var total = 0;
//basically find every selected seat and sum its price
sc.find('selected').each(function() {
total += this.data().price;
});
return total;
}
total = recalculateTotal(scInput);
function call(a) {
window.location.replace("Demo2.jsp?name=" + a);
}
call(total);
只要变量sc和call();你的原始代码样本就可以了。属于同一范围。我认为您可能会被参数与参数混淆。在recalculateTotal函数中,您可以定义一个名为' sc'的参数。但是,当您调用该函数时,您需要发送一个参数(sc的值)。因此,您的原始示例在以下实例中是正确的
var sc = 'your-value'
function recalculateTotal(sc) {
var total = 0;
//basically find every selected seat and sum its price
sc.find('selected').each(function() {
total += this.data().price;
});
return total;
}
function call() {
var a = recalculateTotal(sc);
window.location.replace("Demo2.jsp?name=" + a);
}
call(sc);
在上面的示例中是正确的,因为在调用函数可以访问的全局范围中存在变量sc。这个例子也是如此:
var sc = 'your-value'
function recalculateTotal() {
var total = 0;
//basically find every selected seat and sum its price
sc.find('selected').each(function() {
total += this.data().price;
});
return total;
}
function call() {
var a = recalculateTotal();
window.location.replace("Demo2.jsp?name=" + a);
}
call();
您可能遇到的困惑是,在您的示例中,您在recalculateTotal(sc)中有一个参数 sc ,而在调用方法中,您将我传递给recalculateTotal(sc)的属性也称为sc 。这是正确的符号和一种非常常见的做法,但它确实使那些不熟悉它的人感到困惑