I'm trying to get my head around this black art called JavaScript - and, I must admit, pretty excited about it. I've been looking at code examples, mainly from "easeljs" since that is what I will be using mainly. And I'm a bit confused..
I (think I) understand the difference between using the prototype
for functions or properties that are class
variables and using this.someProp
for 'instance' variables (Yes, I understand that there are no classes in JavaScript.)
The code I have looked at, and am using as templates for my own code, declare
prototype
variables and then refers to them with this i.e.
In the constructor:
this.name = name;
Then a declaration:
Object.prototype.name;
And later,
this.name = "Freddy";
This is within functions called with 'new' so in this case, as I understand it, this
refers to the current object. What puzzles me is what the prototype declaration is doing and why do we use it for instance variables?
Clarification: In the following code, I don't see what the prototype declaration of radius is achieving:
(function(){
// constructor
function MyCircle(radius){
this.radius = radius;
}
MyCircle.prototype.radius;
this.area = function(){
return 3.14*this.radius*this.radius;
};
window.MyCircle = MyCircle;
}());
Yeah, I agree the prototype can be used for default values of properties (variables). The constructor function doesn't need to declare a property; it may be done conditionally.
See how Lucy's
age
is conditionally declared and initialized.The value on a prototype has a key behaviour that is different from a property set directly on the instance. Try this:
This would not possible without prototypical inheritance.
You can test whether the property is the instances property or the prototype property using the
hasOwnProperty
method.An instance can override the
prototype
value.And return to the
prototype
value.This is a powerful part of prototypical inheritance.
In the other pattern:
The
this.name
variable is declared again with every new instance.It makes some sense to have methods as functions declared on the prototype. Rather than the function definition being re-declared on every instance, all instances can share a single function.
In terms of variables, rather than functions, the prototype can possibly be used for default values in the case that an instance does not set its own value.
The code in a fiddle
A value stored on the prototype provides a default value for that property.
If you subsequently write a value to that property, the instance will acquire that new value, hiding the value that's on the prototype, which will be left intact.
In the context of the code you've now added to the question:
does absolutely nothing. It's a no-op - it attempts to read that property and then discards the result.
Other answers have already explained the difference between prototype vs instance properties.
But just to add to the answer, let's break down your code snippet:
IIFE
which acts as a scope container for the inner codeMyCircle
using a constructor pattern (but observe that it never gets "constructed" so should probably get rid of the capital letter since it's misleading)radius
instance property on the invoked objectradius
property onMyCircle
function'sprototype
which doesn't exist so evaluates toundefined
area
instance property on the global window object and assigning it a function expressionMyCircle
instance property on awindow
object and assigning to it theMyCircle
functionSummary: It seems like it's creating an
area
andMyCircle
properties on the globalwindow
object, and whenMyCircle
is invoked it creates an additionalradius
property.Usage: MyCircle should be invoked before area since area relies on MyCircle initialising the radius: