[英]Function with multiple parameters in JavaScript
I just received this test 我刚收到测试
function darling(w) {
/**
* your code here
*/
}
/**
* test functions, those in comment are expected returned value
*/
var demoIsWorking = demo();
var absolutely = darling('w')('i')('l')('l')['y']['o']['u'].m.a.r.r.y('m').e['?'];
var definitely = darling('w')('o')('u')('l')('d')['y']['o']['u'].m.a.r.r.y('m').e['?'];
var rUKidding = darling('w')('i')('l')('l')['y']['o']['u'].m.a.r.r.y('h').im['?'];
var yNot = darling('w')('o')('u')('l')('d')['y']['o']['u'].m.a.r.r.y('h').im['?'];
As you can see from the code when the program call a function darling, it's has many different parameters with different () and []. 从代码中可以看到,当程序调用函数darling时,它具有许多不同的参数,它们具有不同的()和[]。 It's really make me confuse. 真的让我感到困惑。 Can someone explain for me why the function is possible to have many parameters and also many bracket. 有人可以为我解释为什么该函数可能具有许多参数以及许多括号。
Thanks for your help 谢谢你的帮助
A function can return anything, for example an object or another function. 一个函数可以返回任何东西,例如一个对象或另一个函数。 For example: 例如:
function matchesYou() {
return { y: { o: { u: true } } };
}
Makes the following possible: 使以下成为可能:
matchesYou().y.o.u;
Which you can also write as: 您也可以这样写:
matchesYou()['y']['o']['u'];
Or any combination of both. 或两者的任何组合。
For round parentheses, it's even simpler: 对于圆括号,它甚至更简单:
function darling(w) {
return function(iOrU) {
return function(lOrU) {
return function(l) {
return true;
};
};
};
}
You can call that like this: 您可以这样称呼:
darling('w')('i')('l')('l');
Or like this, it doesn't matter: 还是这样,没关系:
darling()()()();
There is only one argument being passed at most. 最多只有一个参数被传递。
What it's doing is it's actually doing chaining different functions by having a function return another. 它实际上是通过使一个函数返回另一个函数来链接不同的函数。
function someFunction(w) {
switch(w): {
case 'a':
return function() { ... }
case 'b':
return function() { ... }
}
}
calling someFunction
may return a completely different function depending on the argument passed. 调用someFunction
可能会返回一个完全不同的函数,具体取决于传递的参数。
Somewhere within those functions, it returns an array within an array 在这些函数中的某个地方,它返回一个数组中的一个数组
[
['a','b','c'],
['a','b','c'],
['a','b','c'],
];
Some of which containing objects 其中一些包含对象
[{a:'1',b:'2' ....}], 'b']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.