0

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?

5
  • Employee.job = job; adds a static property to the function. It won't be accessible by instances of Employee which are created using new Employee(). Why don't you want to change the function? Only thing you can do is prototype inheritance and create a new function Commented May 26, 2020 at 4:30
  • 1
    Are you saying uodate parameter at run time ? Commented May 26, 2020 at 4:41
  • In this case, yes (I'm experimenting in the Chrome debug console), but I'd be interested in finding a solution that works in any scenario. Commented May 26, 2020 at 4:46
  • @adiga I want to add a parameter to the constructor function so job isn't static. The goal is: when I do let employee1 = new Employee (name, job) I can pass whatever name and job I want into the new object. Commented May 26, 2020 at 5:18
  • The best way is to just rewrite the Employee function to the desired code. Why would you want to do this dynamically? Commented May 26, 2020 at 15:20

2 Answers 2

1

you can override the function by adding a wrapper on top of it. Hope this helps.

function Employee(name) {
  this.name = name;
}

function createWrapper(functionToWrap){

 return function EmployeeWrapper(name,job){
   let employeeInstance = new functionToWrap(name);
   employeeInstance.job = job;
   return employeeInstance;
  }

}

Employee = createWrapper(Employee);
employeeInstance = new Employee('name','job');
Sign up to request clarification or add additional context in comments.

Comments

1

Yes, you can do it just as you have described in the question.

Just one change. Adding a variable to the constructor directly like this Employee.job = job; will make it static. Instead add this variable to its object. Check out the following snippet...

<!DOCTYPE HTML>
<html>
    <body>

    <h2>JavaScript Object Constructors</h2>

    <p id="demo"></p>

    <script>
        // Constructor function for Person objects
        function Person(first, last, age, eye) {
            this.firstName = first;
            this.lastName = last;
            this.age = age;
            this.eyeColor = eye;
        }

        // Create 2 Person objects 
        var myFather = new Person("John", "Doe", 50, "blue");
        var myMother = new Person("Sally", "Rally", 48, "green");

        // Add nationality to first object
        myFather.nationality = "English";

        // Display nationality 
        document.getElementById("demo").innerHTML =
        "My father is " + myFather.nationality; 
    </script>

    </body>
</html>

4 Comments

@flightPlan did it solve your problem? If yes, then do accept the answer. Let me know if you need anything else.
Hi @Deepak Thank you for your answer, but I don't think this quite solves it. Using your example: I'd like to add a parameter to the "constructor function for Person objects" itself (after it's already been initialized). So whenever I'm using new Person The new person accepts the argument "nationality". The first line of the constructor function would be function Person(first, last, age, eye, nationality).
so, you want to alter the constructor definition and want to use the updated construction thereafter, right?
Correct. Ideally without overwriting the function.

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.