I am new in JS programming and trying to understand prototype-based inheritance. below is my test code and I have a question about line method 'parseParameters'.
As I know, when I am instantiating class Point and wring following:
var p = new Point ({x: 1, y: 1});
alert(p.x);
member 'x' firstly searched in the Point class, then in its prototype (Shape). Is it right?
And question itself: where will be created members 'x' and 'y' - in the Point class or in Shape (prototype)?
One remark: should I actually thinking of it? Maybe it is negligible question and there is no matter where the member created?
var Shape = function () {}
Shape.prototype = {
getParameter: function (params, name) {
return params !== null && params !== undefined
? params[name]
: 0;
},
parseParameters: function(params, names) {
if (params === null || params === undefined) {
return;
}
for(var i = 0; i < names.length; i++) {
this[names[i]] = params[names[i]];
}
}
}
var Point = function (params) {
this.parseParameters(params, ['x', 'y'])
}
Point.prototype = new Shape;