vl - more on prototypes
Prototype Object in JavaScript
Functions as Objects
In JavaScript, functions are treated as objects.
Functions can have properties and methods.
Link Property
There is a property called
lengththat indicates the number of arguments a function can accept.Example: If a function is defined to take two arguments, the result of
functionName.lengthwill yield2.
Prototype Property
Every function has a
.prototypeproperty, which is initially an empty object.Accessing Prototype: Using
typeof foo.prototypereturnsobject, indicating it is an object type.Properties and methods can be added to this prototype.
Using Prototype in Constructors
For any properties or methods added to the prototype to be effective, the function must be called as a constructor (using
newkeyword).Example: Create a function named
gadgetthat accepts parametersnameandcolor.Adding properties:
Gadget.prototype.price = 100;Adding methods:
Gadget.prototype.getInfo = function() {...}.When an instance is created using the new keyword, it can access prototype properties and methods.
Example:
var newToy = new Gadget();Accessing price:
newToy.pricewould return100.
Live Nature of Prototypes
The prototype is 'live,' meaning changes to the prototype will affect all existing instances, even those created before changes were made.
If a new method is added to the prototype, it will be available on all existing instances of the object type.
Checking Prototypes -
isPrototypeOfMethodTo determine if a prototype is used by an object, the
isPrototypeOfmethod can be employed.Example:
Create a constructor function called
humanthat takes one argument,name.Set the prototype of
humanto another object (e.g.,monkey).When creating a new
humanobject, the statementmonkey.isPrototypeOf(george)returnstrueif 'george' is an instance ofhuman.
Prototype Chain
If a property or method isn’t found on an object, JavaScript checks the prototype chain to find it.
Example: If
developerdoes not have atoStringmethod, the engine will checkhuman, thenmonkey, and finallyobjectfor that method.
Creating Instances and Adding Properties
Properties can also be added directly to object instances.
Example: Define a new object
developeras an instance ofhuman.Example of adding property:
developer.drinks = 'Red Bull';If
drinksdoes not exist onhuman, prototype inheritance allows it to be accessed if defined onmonkey.
Modifying Built-in Objects
JavaScript allows you to augment built-in types by adding methods to their prototypes. This can enhance functionality.
Example: Add a method to the
Array.prototypefor searching within arrays.Define
Array.prototype.contains = function(needle) { ... };to check for an item in the array.Use a loop, ensuring to minimize performance overhead by storing
this.lengthin a variable rather than callingthis.lengthrepeatedly.
Caution on Prototype Modifications
Although enhancing prototypes can be powerful, it should be done carefully and judiciously.
Use this technique sparingly, especially when beginning, to avoid unintended consequences.
Conclusion
Future discussions will cover actual inheritance in JavaScript, building on these principles.