Javascript novice here. There's been a lot written on this subject, but I can't find the answer (so what I'm trying to do is probably bad form):
I can add a property to an existing function, but I'd like to know if I can add a new parameter to a function (without just overwriting the function).
Take an existing function (I happen to be using this one as a constructor function):
function Employee(name) {
this.name = name;
}
I'd like to add a "job" property. I could write
Employee.job = job;
But Employee only has one parameter ("name"). I want to give the Employee function an additional parameter: "job".
So the desired end result would look like:
function Employee(name, job) {
this.name = name;
this.job = job;
}
Is there a way to do this?
Employee.job = job;adds a static property to the function. It won't be accessible by instances ofEmployeewhich are created usingnew Employee(). Why don't you want to change the function? Only thing you can do is prototype inheritance and create a new functionjobisn't static. The goal is: when I dolet employee1 = new Employee (name, job)I can pass whatever name and job I want into the new object.Employeefunction to the desired code. Why would you want to do this dynamically?