javascript - Why are some object methods declared differently than others -
so have this code 1 of examples class.
the method created point.new method.
point.prototype.tostring = function() { return "(" + this.x + "," + this.y + ")"; }; point.new = function(x,y) { var newobj = object.create(this.prototype); this.call(newobj, x,y); return newobj; };
what don't understand why don't need declare method
point.prototype.new = function(){}
(in fact wont compile when that)
point.prototype.tostring(){}
method necessary. in both cases adding new method point object, how come 1 method being called on point , other being called on point.prototype (i believe points object?)
i'm not sure rule falls under, remember looking @ "adding property prototype rule" here.
but in case point not prototype since has no instances right?
the prototype used define methods can called on object created new point
. e.g. can do:
var x = new point(15, 20); var str = x.tostring();
but wouldn't use x.new()
, because .new()
creating new objects, doesn't apply existing objects. defining point.new
creates function can called as:
var y = point.new(10, 20);
Comments
Post a Comment