Adding new .this property to constructor?

So javascript rookie is curious about something.

Let’s say I create a constructor

let Person = function(name, job, age){
this.name = name;
}

But let’s say I forget to add this.job and this.age inside the function and I can’t fix it manually. Is it possible to add those two this. properties later on without using prototype?

short answer No! :wink:

1 Like

If your question is, can methods added to the Person later get access to variables from the constructor, then the answer is no.

const Person = function(name, job, age){
  // here we have created a property, called name
  this.name = name;
  // We also have three local variables, 'name','job' and 'age'. They are **only**
  //   accessible to this function, or to functions defined within it:
  this.addMyJob = function(){
    this.job = job; 
    // This line would work - we have defined the function in the same context (or 'scope')
    //   as the variable itself.
  }
}

Person.prototype.addJob = function(){
  // here, we can't see the 'job' variable we created in the constructor - that's a private
  //  variable in the 'Person' namespace, and this won't be able to access it!
  this.job = job;
  //  The above will throw an error, as 'job' is not defined within the context of addJob
}

const bob = new Person('Bob','worm rancher',102);
console.log(JSON.stringify(bob, null, '  ') );

// Now, let's try that addMyJob:
bob.addMyJob();
console.log(JSON.stringify(bob, null, '  ') );

Click the play arrow on the repl code below, to see that work. It isn’t exactly what you’re looking for, but it explains that only functions in the same context as the variables have access to those variables.

1 Like

Not exactly what I meant, if I was trying to add properties to on object I would do it like you showed in the example above, but what I meant was is it possible to add this.job = job and this.age = age into the constructor later on.

Your method would work for the object John but I would still had to add job and age to every new object manually instead of just putting it into constructor Person parametars and using said constructor to create new Objects.

I guess this may be a silly question but I was working on some exercises and It made me think.

1 Like

Like I wrote above I didn’t mean methods just basic properties like this.job = job and this.age = age. Still thanks for help, I wasn’t even thinking abotu methods but I learned something new regardless.

Thanks folks!

1 Like

It isn’t a silly question at all. It’s actually one of the very deep, dark areas of javascript. You’re now getting into functional scope and closures and things like that. It’s the stuff that gets you thinking.

As I showed, the issue is that the parameters you pass in are only available inside that Person constructor function, where they’ve been locally named ‘name’, ‘job’ and ‘age’, so only stuff defined in there can access them. The short answer to your question was no lol - the rest was the LONG answer. :wink:

1 Like