在 PHP 中,我们可以在函数中指定变量的默认值,例如:
function myFunction(myDefaultVariable, myOtherVariable, myCheckVariable = "basic"){
// so yeah, myDefaultVariable is required always,
// same applies for myOtherVariable,
// and myCheckVariable can be skipped in function call, because it has a default value already specified.
}
JavaScript中有类似的东西吗?
您不需要传递 Javascript 中的所有变量。
尽管一种不那么麻烦的方法是使用对象:
function foo(args) {
var text = args.text || 'Bar';
alert(text);
}
调用它:
foo({ text: 'Hello' }); // will alert "Hello"
foo(); // will alert "Bar" as it was assigned if args.text was null
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)