I have a constructor function as shown below:
var Person = function (name, age) {
this.name = name;
this.age = age;
}
Person.prototype = {
getAgePlusTwo: function () {
return this.age + 2;
}
}
I can create such an object and access instance methods:
var p1 = new Person('Person1', 22);
p1.getAgePlusTwo();
However, I get my objects from the JSON service, so I cannot use newto create objects. Is there a way to map an object literal like this:
var p2 = { name: 'Person2', age: 25 }
to be an instance Person, so I can use its instance methods like p2.getAgePlusTwo()?
source
share