This way of thinking is useful because it helps us to understand that a
JavaScript object definition (i.e. a class in object oriented
terminology) is any function that assigns itself properties.
In Javascript, the word "class" is, like "method", only something that
exists in the eyes of the user. Internally, a method is just a property
that happens to be a function. And "class" is usually used about a function
that happens to be used as a constructor.
Examples of built-in constructor functions: Object, Array, Number,
String, Date, RegExp. These are *functions*, but if you use them with
the "new" operator, they generate objects that have similar
structures. The notation ("new Foo()") and use (creating new similar
objects) make us think about them as we would about classes in
class-based object oriented languages. But in Javascript, any function
can be a constructor/class.
Example:
function Point(x,y) {
this.x=x;
this.y=y;
}
This function is meant to be a constructor. When you use it as
new Point(10,20)
the "new" operator creates a new object, and calls Point so that the
"this" keyword refers to the new object. It calls Point just as any
other function.
You could get the same result with "Point.call(new Object(),10,20)",
(in this case only, there are more details to the "new" operator).
You can write
new func()
for any function func.
A prototype is an object property that belongs to a constructor function
I.e., any function. Any Javascript function initially has a property
called "prototype", that refers to an object.
and is automatically created when the function begins assigning
properties to itself.
It is created when the function is.
var foo = function(){};
alert(typeof foo.prototype);
The prototype is JavaScript's way of remembering
what properties an object definition has. Object definitions (i.e.
constructor functions) may inherit or copy the prototype of other object
definitions - meaning that a constructor may define itself as having the
same set of properties as another constructor.
This got me confuzed. Constructor functions (i.e., any function) uses
their prototype object for properties that will be shared by the
created objects. They are not properties of the constructor itself.
Any user-created Javascript object has an prototype reference. When
you look for a property, say "obj.prop", then the property is looked
for in the object itself. If it is not found, it is looked for in the
object's prototype, and so forth.
When you create a new object with the "new" operator and a function,
the new object's prototype reference will point to the value of the
function's prototype property.
Try looking at what happens here:
---
function Foo(){};
Foo.prototype.x = 42;
var x = new Foo();
alert(x.x); // 42
x.x = 37;
alert(x.x); // 37
delete x.x; // remove the property from the object
alert(x.x); // 42 again, the prototype wasn't affected.
Foo.prototype.x = 37;
alert(x.x); // 37. The prototype object can be changed dynamically too.
Foo.prototype = {x:4,y:87}; // overwrite prototype with new object
var y = new Foo();
alert(x.x); // 37, the prototype reference of old objects are unchanged
alert(y.x); // 4
alert(y.y); // 87