1

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");
4
  • Otherwise you will get an error in your browser's console. Try the same. Commented Feb 22, 2018 at 12:16
  • 2
    Your function will not work, you've initialized employees, but not emp. In JavaScript, attempting to assign a value to a property of an undefined object is not possible and will throw an error. Commented Feb 22, 2018 at 12:16
  • Your typo in the question (using emp instead of employees) answers your question for you: "Uncaught TypeError: Cannot set property 'Firstname' of undefined" Commented Feb 22, 2018 at 12:16
  • I suggest you work through more basic JavaScript tutorials and/or a good solid beginner's book. You have to create the object because nothing else is going to create it for you. If you meant your function to be a constructor function (used with new), then new would create an object for you, which you'd access in the function via this. Commented Feb 22, 2018 at 12:18

2 Answers 2

1

If I understand your question correctly. You could just return object literal w/o creating a temporary variable.

function allemployees (Firstname,Lastname,Gender,Designation) {
    return {
      Firstname: Firstname,
      Lastname: Lastname,
      Gender: Gender,
      Designation: Designation
    };
}

console.log(allemployees("John", "Miller", "M", "abc"))

Or with ES2015 enhanced object literals the syntax is even shorter.

function allemployees (Firstname, Lastname, Gender, Designation) {
    return { Firstname, Lastname, Gender, Designation };
}

console.log(allemployees("John", "Miller", "M", "abc"))

Sign up to request clarification or add additional context in comments.

1 Comment

@Rahul99 Glad I could help. Also I do recommend you to take TJ Crowder's advise to read a tutorial about OOP in js: constructors, prototypes, etc.
-1

First of all there is an error in your code. You declared and initialized employees variable and trying to assign value to emp variable which is not yet declared and thus undefined error.

Try this one

function allemployees (Firstname,Lastname,Gender,Designation) { return {"Firstname":Firstname, "Lastname":Lastname, "Gender": Gender, "Designation": Designation}; }

This will work for you without declaring any variable and will return what you want.

Also what I am getting from you comments is that you have employees data from some where else and want to store that data in javascript object. So you declare and initialized employees variable at the calling point of function allemployees(....).

var employees = {}; employees.push(allemployees("John", "Miller", "M", "abc"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.