1

I have the following Javascript code;

var Person = function(name, age) {
    this.name = name;
    this.age = age;
    return this;
};

Person.prototype.getAge = function() {
    alert("Age : " + this.age);
}

var p1 = new Person("xyz",10);
p1.getAge();

This works perfectly and I get the alert as Age : 10

Now if I update the code as below (defined getAge() after instantiating Person object p1);

var Person = function(name, age) {
    this.name = name;
    this.age = age;
    return this;
};

var p1 = new Person("xyz",10);

Person.prototype.getAge = function() {
    alert("Age : " + this.age);
}

p1.getAge();

It still returns me the output as "Age : 10"

Now my question is how does this work correctly, since Person.prototype.getAge was defined after we have instantiated Person object p1 ? Is it because of the way "prototype" works ?

3 Answers 3

1

Objects in javascript have a sort of a chain where if you ask them for a property, they will return theirs and if they don't have it, it'll try to find it above its chain, in your case, when p1 is asked for its getAge property, it looks for it in Person.prototype.

var Person = function(name, age) {
    this.name = name;
    this.age = age;
    return this;
};

var p1 = new Person("xyz", 10);
var p2 = new Person("abc", 12);

Person.prototype.getAge = function() {
    alert("Age : " + this.age);
}
p1.getAge = function() {
    alert("I am " + this.age + " years old.");
}

p1.getAge(); // I am 10 years old.
p2.getAge(); // Age : 12

In this example, p1 has its own getAge property, so when asked, it returns it. On the other hand, p2 doesnt really have this property but can access it via "that chain" and returns the property of its prototype.

Sign up to request clarification or add additional context in comments.

Comments

1

Yes that is the way prototype works. You can use this technique to extend strings, objects and arrays too.

Comments

1

Any object prototype can be modified at any time. Think about the libraries/frameworks that modify the base Object prototype...even the built in objects (Date, String, etc) will all have the modifications done to Object.prototype whether or not they happen immediately at page load

try this:

Object.prototype.myTest = function() { console.log('hi'); }
var a = new Date()
a.myTest(); //hi

3 Comments

I was about to upvote, but please first don't extend Object.prototype, which is totally not recommended, unless you really really really know what you're doing...
@MaxArt I might modify that to say, "Don't extend Object.prototype with an enumerable property unless you really mean it. Usually custom methods should not be enumerable, and so should be defined using Object.defineProperty()."
@MaxArt: I'm not recommending that extending Object.prototype is a good idea, merely as an example for the OP demonstrating that the prototype may be modified at any time

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.