To do what you require you need to accept a single object as an argument.(要执行您需要的操作,您需要接受一个对象作为参数。)
popupModal({ param2: 'foo', param6: 'bar' }); function popupModal(settings) { console.log(settings.param2); console.log(settings.param6); }
If you wanted some parameters to be optional then you can define an object within your function which contains the default values.(如果希望某些参数是可选的,则可以在函数中定义一个包含默认值的对象。) Then you can use jQuery's $.extend()
method to overwrite it with the user defined settings:(然后,您可以使用jQuery的$.extend()
方法用用户定义的设置覆盖它:)
popupModal({ param2: 'foo', param6: 'bar' }); function popupModal(settings) { var defaults = { param1: '1', param2: '2', param3: '3', param4: '4', param5: '5', param6: '6', } settings = $.extend(defaults, settings); console.log(settings.param1); console.log(settings.param2); console.log(settings.param3); console.log(settings.param4); console.log(settings.param5); console.log(settings.param6); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…