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

77
Views
Object.Create calling function with prototype

Oke, I have this working:

const personProto2 = {
  calAge() {
    console.log(2021 - this.birthday);
  },
};
const rein = Object.create(personProto2);
rein.name = "Rein";
rein.birthday = 1945;
rein.calAge();

But if I do this:

const Person = function (name, birthday) {
  this.name = name;
  this.birthday = birthday;
};

Person.prototype.calAge = function () {
  console.log(2021 - this.birthday);
};
const rein = Object.create(Person);
rein.name = "Rein";
rein.birthday = 1945;
rein.prototype.calAge();

It doesn't work. But a function is also a object. And a object has also a prototype.

So why the second example doesn't work?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think that you meant using new when creating a blank, plain JavaScript object.

The new operator lets you create an instance of a user-defined object type or of one of the built-in object types that has a constructor function. now, you can call the calAge method.

function Person(name, birthday) {
  this.name = name;
  this.birthday = birthday;
};

Person.prototype.calAge = function () {
  console.log(2021 - this.birthday);
};

const rein = new Person("Rein", 1945);
rein.calAge();

about 4 years ago · Juan Pablo Isaza Report
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!