constructor function prototype javascript code example
Example 1: function prototype javascript
function Person(name) {
this.name = name;
}
Person.prototype.getName = function() {
return this.name;
}
var person = new Person("John Doe");
person.getName()
Example 2: javascript prototype vs constructor function
function Class () {}
Class.prototype.calc = function (a, b) {
return a + b;
}
var ins1 = new Class(),
ins2 = new Class();
console.log(ins1.calc(1,1), ins2.calc(1,1));
Class.prototype.calc = function () {
var args = Array.prototype.slice.apply(arguments),
res = 0, c;
while (c = args.shift())
res += c;
return res;
}
console.log(ins1.calc(1,1,1), ins2.calc(1,1,1));