Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

423
Views
What does 'new' do to modify the `this` object in a function?

What does the new keyword do that allows the following binding on the this object:

function Person(first, last) {
    this.first = first;
    this.last = last;
}
Person.prototype.greet = function() {
    console.log(`Hello, ${this.first} ${this.last}.`);
};
let p = new Person('Tom', 'Jones');
p.greet();

Whereas without the new keyword, this.first would raise an error since it is undefined. Conceptually speaking, what is new doing here in how it modifies the function?

Without new:

function Person(first, last) {
    this.first = first;
    this.last = last;
}
Person('Tom', 'Jones');
// TypeError: Cannot set property 'first' of undefined


And without using new seems the following is a crude way to simulate it:

function Person(first, last) {
    this.first = first;
    this.last = last;
    return this;
}
Person.prototype.greet = function() {
    console.log(`Hello, ${this.first} ${this.last}.`);
};

Person.prototype.greet.call(Person.call({}, 'Tom', 'Jones'));

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!