0

Possible Duplicate:
JavaScript variable number of arguments to function
JS: functions arguments default values

I want to write this sort of function that accepts various number of parameters like this:

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

My question is how do I write the function itself? I would have tried something like this but I assume it won't work. how do I write it correctly so the function can receive multiple parameters?

var PopupBox = {
    init :function (width,height) {
        if (height == null) {
           height=100px;
        }
        if (width == null) {
           height=100px;
        }
    }
}
Community
  • 1
  • 1
Alon
  • 7,618
  • 18
  • 61
  • 99
  • 5
    You've written the code. Why not test it instead of assuming it won't work? – Quentin May 28 '12 at 15:19
  • 1
    I thought that there is not a chance that it would work - I only gave this code as an example. but once tested it - it did work! but why? how can it take multiple parameters like that? – Alon May 28 '12 at 15:27
  • 1
    Any argument that has a placeholder but isn't passed a value gets `undefined`. – Quentin May 28 '12 at 15:29

0 Answers0