如何? (ES6+)
function foo({x, y}){
document.write("X is " + x);
document.write("Y is " + y);
}
并称:
foo({x:10, y:5})
There is a downside to using a single structured argument over multiple arguments, and that is with multiple arguments you can use /**
in may IDEs to generate a method header which will display an @param
for each argument.
But if you only have one argument then you will lose the niceness of a description for each argument and hence less useful intelli-sense in the IDE as it wont pick up the docuemntation of the structure s properties.
/**
* Do stuff
* @param {*} param0 - A structure containing the blah, blah blah data
*/
function foo({x, y}){
而不是。
/**
*
* @param {*} x - The value for blah
* @param {*} y - the value for blah-blah
*/
foo1(x, y){