February 2015
Intermediate to advanced
124 pages
2h 31m
English
A prototype is an object whose properties are shared by all objects that have that prototype. An object’s prototype can usually be accessed using the aptly named prototype property, though there are exceptions.[34]
However, you can’t just go and write A.prototype = B. Instead, you need to use the new keyword, which takes a function and creates an object that “inherits” that function’s prototype. When a function is used this way, it’s referred to as a constructor. Here’s a quick example:
| | Boy = -> # by convention, constructor names are capitalized |
| | Boy::sing = -> |
| | console.log "It ain't easy being a boy named Sue" |
| | sue = new Boy() |
| | sue.sing() |
Here, Boy::sing is shorthand for Boy.prototype.sing. The :: symbol ...
Read now
Unlock full access