一次将多个对象传递给函数参数
Passing multiple objects to function parameter at once
我正在编写一个程序(在 JavaScript 中),它将根据用户的需求确定最合适的汽车。我有 4 个对象,每个对象都是不同的汽车。为简化起见,我将提供一个对象作为示例。假设我的代码中还有 3 个具有相同属性的其他对象(菲亚特、奥迪、宝马等)。
var chevy = {
make: "Chevy",
model: "Bel Air",
year: 1957,
color: "red",
passengers: 2,
convertible: false,
mileage: 1021
};
目标是将每个对象作为参数传递给函数,return 基于条件的布尔值。这是函数:
function prequal(car) {
if (car.mileage > 10000) {
return false;
}
else if (car.year > 1960) {
return false;
}
else {
return true;
}
}
并调用函数:
var worthALook = prequal(taxi);
if (worthALook) {
console.log("You gotta check out this " + taxi.make + " " + taxi.model);
}
else {
console.log("You should really pass on the " + taxi.make + " " + taxi.model);
}
我必须分别调用每个对象吗?或者是否有一种简化的方法可以同时调用所有 4 个对象的函数?我是新手,解决这个问题激起了我的好奇心。
谢谢!!
编辑:对不起,我的杂乱无章,但我似乎找到了解决办法。我正在使用嵌套函数获得所需的输出:
function worthALook(car) {
var shouldYouBuy = prequal(car);
if (shouldYouBuy) {
console.log("You gotta check out this " + car.model);
}
else {
console.log("You should really pass on this " + car.model);
}
}
在 'worthALook' 函数输出中调用原始 'prequal' 函数(见上文):
You should really pass on this Taxi
You should really pass on this Cadillac
You gotta check out this Bel Air
You should really pass on this 500
在每个对象之后我调用了 worthALook 函数,如下所示:
worthALook(chevy);
worthALook(fiat);
等等
我收到了我想要的输出,但我的代码是不是有点矫枉过正?
谢谢!
您可以将对象放在 Array
中并筛选出值得一看的内容。 Return 该对象而不是布尔值。
function worthALook(){
return ([].filter.call(arguments, prequal) || [false])[0];
/* remove [0] from the above line and change [false] to false if you
want to return multiple cars that satisfy a condition. Use indexes if you do that */
}
var car = worthALook(chevy, fiat, audi, bmw); // pass inas many as you want
if(car) // check if it's a car
console.log("You gotta check out this " + taxi.make + " " + taxi.model);
return语句与||
一起使用,所以即使none辆汽车满足条件,那么你也可以returnfalse
。此外,我们正在使用 arguments
对象,因此您可以将尽可能多的 car
对象传递给 worthALook
函数,该函数将使用 prequal
函数来过滤它们。
我正在编写一个程序(在 JavaScript 中),它将根据用户的需求确定最合适的汽车。我有 4 个对象,每个对象都是不同的汽车。为简化起见,我将提供一个对象作为示例。假设我的代码中还有 3 个具有相同属性的其他对象(菲亚特、奥迪、宝马等)。
var chevy = {
make: "Chevy",
model: "Bel Air",
year: 1957,
color: "red",
passengers: 2,
convertible: false,
mileage: 1021
};
目标是将每个对象作为参数传递给函数,return 基于条件的布尔值。这是函数:
function prequal(car) {
if (car.mileage > 10000) {
return false;
}
else if (car.year > 1960) {
return false;
}
else {
return true;
}
}
并调用函数:
var worthALook = prequal(taxi);
if (worthALook) {
console.log("You gotta check out this " + taxi.make + " " + taxi.model);
}
else {
console.log("You should really pass on the " + taxi.make + " " + taxi.model);
}
我必须分别调用每个对象吗?或者是否有一种简化的方法可以同时调用所有 4 个对象的函数?我是新手,解决这个问题激起了我的好奇心。
谢谢!!
编辑:对不起,我的杂乱无章,但我似乎找到了解决办法。我正在使用嵌套函数获得所需的输出:
function worthALook(car) {
var shouldYouBuy = prequal(car);
if (shouldYouBuy) {
console.log("You gotta check out this " + car.model);
}
else {
console.log("You should really pass on this " + car.model);
}
}
在 'worthALook' 函数输出中调用原始 'prequal' 函数(见上文):
You should really pass on this Taxi
You should really pass on this Cadillac
You gotta check out this Bel Air
You should really pass on this 500
在每个对象之后我调用了 worthALook 函数,如下所示:
worthALook(chevy);
worthALook(fiat);
等等
我收到了我想要的输出,但我的代码是不是有点矫枉过正? 谢谢!
您可以将对象放在 Array
中并筛选出值得一看的内容。 Return 该对象而不是布尔值。
function worthALook(){
return ([].filter.call(arguments, prequal) || [false])[0];
/* remove [0] from the above line and change [false] to false if you
want to return multiple cars that satisfy a condition. Use indexes if you do that */
}
var car = worthALook(chevy, fiat, audi, bmw); // pass inas many as you want
if(car) // check if it's a car
console.log("You gotta check out this " + taxi.make + " " + taxi.model);
return语句与||
一起使用,所以即使none辆汽车满足条件,那么你也可以returnfalse
。此外,我们正在使用 arguments
对象,因此您可以将尽可能多的 car
对象传递给 worthALook
函数,该函数将使用 prequal
函数来过滤它们。