Safe Programming
Safe Programming
Set initial values for all variables right when you define them:
var reservationStatus = 0;
Set values of object properties as soon as you create an object instance:
var reservation
= new Object();
reservation.status = 0;
reservation.date_start = $('reserve.date_start');
reservation.date_end = $('reserve.date_end');
When you store the object as a property of the browser window, initially the property is not defined. This does not mean that the property has a null value – it means that the property simply does not exist. Use the following pattern in your code:
if (typeof window.reservation
== 'undefined') {
// the object does not exist yet, let’s create a new
one
window.reservation = new Object();
window.reservation.res_id = -1;
} else {
// the object exists and can be used
alert(window.reservation.res_id);
}