0

So I have this code from one of the examples from class.

The method we created is the 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 I don't understand is why we don't need to declare the method as

point.prototype.new = function(){}

(in fact it wont compile when i do that) however for the

point.prototype.toString(){} 

method it is necessary. In both cases we are adding a new method to the point object, so how come one method is being called on just point and the other is being called on point.prototype (I believe this points to object?)

I'm not sure what rule this falls under, but i remember looking at the "adding property to prototype rule" here.

but in this case point is not a prototype since it has no instances right?

Steven Hsu
  • 183
  • 1
  • 3
  • 15
  • 1
    w3schools is not a good place to learn, unfortunately: http://www.w3fools.com/ – Dai Oct 15 '15 at 04:37
  • Read this question and answer: http://stackoverflow.com/questions/8433459/js-why-use-prototype – Taylor H. Oct 15 '15 at 04:41
  • Another request to ignore w3schools, much of the content on the site is misleading or plain wrong, e.g. *The Object.prototype is on the top of the prototype chain*. No it isn't, *null* is. *All JavaScript objects … inherit from the Object.prototype.* Nope, host objects don't have to, Object.prototype doesn't and native objects can be created that don't. – RobG Oct 15 '15 at 04:54
  • Do a search for [*MDN prototype inheritance*](https://www.google.com.au/search?q=MDN+prototype+inheritance&gws_rd=cr,ssl&ei=NDMfVpqrI8a-0ASK5pKYBw) and read artciles like [*Inheritance and the prototype chain*](https://developer.mozilla.org/en/docs/Web/JavaScript/Inheritance_and_the_prototype_chain) – RobG Oct 15 '15 at 05:03

2 Answers2

1

The prototype is used to define methods that can be called on any object created with new Point. E.g. you can do:

var x = new Point(15, 20);
var str = x.toString();

But you wouldn't use x.new(), because .new() is for creating new objects, it doesn't apply to existing objects. Defining Point.new creates a function that can be called as:

var y = Point.new(10, 20);
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

In both cases we are adding a new method to the point object,

No, you're not.

Point.prototype.toString sets a function on the Point prototype - this makes it a property-function that will be inherited by all Point value instances, it is an instance property-function because it makes use of the this value to access the instance values x and y.

Point.new is not an instance-level function-property, it is a Constructor-level function-property, this is JavaScript's equivalent to a static function in languages like Java or C#. The Point.new function does not access any instance values, instead it serves as a factory method. It does use the this keyword but that's because this in JavaScript is a bit funny (in this case, it's referring to the current Function object, where the call property lives.

Dai
  • 141,631
  • 28
  • 261
  • 374