Reputation: 4492
I was reading a script and noticed that the programmer who wrote it had declared all their variables within an object. Example:
var variables = {
height : window.innerHeight,
width : window.innerWidth,
image : 'img/image.png'
}
Why would one set their variables out like this instead of the traditional method (as seen below)?
var height = window.innerHeight,
width = window.innerWidth,
image = 'img/image.png';
Upvotes: 1
Views: 85
Reputation: 2400
It depends on the usage.
Perhaps the programmer wants to pass the fields height
, width
and image
into a function, so he placed these variables inside an object to pass it as one parameter.
so instead of doing this
function doSomething(height, width, image) {
// do something
}
he can do this
function doSomething(imageObject) {
// do something
}
Upvotes: 1