比方说,我有一个嵌套函数,名为countries,我想在函数countries中调用两个函数,其中一个名为俄国的参数名为citys1,另一个名为中国的参数名为citys2。如何在嵌套函数中使用参数调用这两个函数?
$input = $request->all();
$valid = ['valid'];
foreach($input as $key => $val)
{
if(!in_array($key,$valid)) abort(404);
}
答案 0 :(得分:1)
我认为您可以使用对象(如类)。
下面的代码呢?
var countries = {
getRussia: function(cities1) {
var totalpop1 = cities1 * (10 ** 6);
//each city has a population of 10^6 people
return totalpop1;
},
getChina: function(cities2) {
var totalpop2 = cities2 * (10 ** 6);
//each city has a population of 10^6 people
return totalpop2;
},
getTotal: function(pop1, pop2) {
var result = this.getRussia(pop1) + this.getChina(pop2);
return result;
}
}
var div = document.querySelector("#result");
div.innerHTML = countries.getTotal(1, 4);
<div id="result"></div>
但是,如果您真的想调用嵌套函数,则可以使用闭包:
function countries() {
return function(cities1) {
var totalpop1 = cities1 * (10 ** 6);
return function(cities2) {
var totalpop2 = cities2 * (10 ** 6);
return totalpop1 + totalpop2;
}
}
}
var div = document.querySelector('#result');
div.innerHTML = countries()(1)(4);