ChapterÂ 8.Â Function Prototype Property
Conceptual Overview of the Prototype Chain
prototype property is an
Function() instance. Specifically, it links
object instances created with the
keyword back to the constructor function that created them. This is done
so that instances can share, or inherit, common methods and properties.
Importantly, the sharing occurs during property lookup. Remember from
ChapterÂ 1 that every time you look up or
access a property on an object, the property will be searched for on the
object as well as the prototype chain.
A prototype object is created for every function, regardless of whether you intend to use that function as a constructor.
Below, I construct an array from the
Array() constructor, and then I invoke the
<!DOCTYPE html><html lang="en"><body><script> var myArray = new Array('foo', 'bar'); console.log(myArray.join()); // logs 'foo, bar' </script></body></html>
join() method is not defined
as a property of the
instance, but somehow we have access to
join() as if it were. This method is defined
somewhere, but where? Well, it is defined as a property of the
prototype property. Since
join() is not found within the array object
Okay, so why are things done this way? Really, it is about efficiency and reuse. Why should every array instance created from the ...