可能重复:
JavaScript variable number of arguments to function
JS: functions arguments default values
我想写这种接受各种参数的函数,如:
PopupBox.init() //Creating a pop up box with a default height and width of 100px
PopupBox.init(50px) //Creating a popup with height and width of 50px
PopupBox.init(50px,100px) //Creating a popup with the height 50px and width 100px
我的问题是如何编写函数本身? 我会尝试这样的东西,但我认为它不会起作用。如何正确编写,以便函数可以接收多个参数?
var PopupBox = {
init :function (width,height) {
if (height == null) {
height=100px;
}
if (width == null) {
height=100px;
}
}
}