Why is it necessary to create and use an empty object at the beginning of the function(like employees in this case). Is there any other way to write this object in this function. This is probably a stupid question but I'm a newbie in javascript.
function allemployees (Firstname,Lastname,Gender,Designation) {
var employee = {};
employee.Firstname = Firstname;
employee.Lastname = Lastname;
employee.Gender = Gender;
employee.Designation = Designation;
return employee;
}
var emp = allemployees("John", "Miller", "M", "abc");
employees, but notemp. In JavaScript, attempting to assign a value to a property of an undefined object is not possible and will throw an error.empinstead ofemployees) answers your question for you: "Uncaught TypeError: Cannot set property 'Firstname' of undefined"new), thennewwould create an object for you, which you'd access in the function viathis.