A prototype is used to declare properties and methods for a class of objects. One advantage of using prototype is that it conserves memory because all instances of a class point to the properties and methods of the prototype which conserves memory and effectively allows properties to be treated as static by all instances of a class.
Prototype is used for inheritance through prototype chaining.
My question is very simple. Why use prototype at all when you can just do:
function car() {
this.engine = "v8";
}
function mustang() {
// nm, no good way to inherit without using prototypes
}
Is that right? So the primary purpose of prototypes is threefold:
Prototypes allow you to easily define methods to all instances of a particular object. The beauty is that the method is applied to the prototype, so it is only stored in the memory once, but every instance of the object has access to it.
Prototype model should be used when the desired system needs to have a lot of interaction with the end users. Typically, online systems, web interfaces have a very high amount of interaction with end users, are best suited for Prototype model.
You could potentially "create" such an object by instantiating a new environment ("realm" in ES6 terms), e.g. via an <iframe> , capturing its Object. prototype , stripping it of its properties and voilá - you've got a new empty object.
Every object in JavaScript has a built-in property, which is called its prototype. The prototype is itself an object, so the prototype will have its own prototype, making what's called a prototype chain. The chain ends when we reach a prototype that has null for its own prototype.
conserve memory
Yes it does, when you create hundreds of instances of Car and they all have their own functions (that have their own closure scopes) you'll consume more memory.
Can't find a reference for it but it has been suggested that Chrome optimises constructor functions that use prototype better than constructor functions with everything in the constructor body.
provide static properties
Static is more like Date.now()
, every instance has members from the prototype but can be called on the instance.
is the only way for a reference type to inherit from a super class
You can inherit with Parent.apply(this,arguments);
in Child but it makes extending Parent functions more complicated and doesn't make childInstance instanceof Parent
true. What that code does is run Parent code with the to be created Child instance as the invoking object (this
). Inheritance is usually done in 2 places.
Parent.apply(this,arguments);
to re use Parent initialisation code and make Parent instance members to be Child instance members (for example: this.name).Child.prototype=Object.create(Parent.prototype);Child.prototype.constructor=Child;
This will ensure that shared Parent members are available on Child instances (like the function getName).These points are explained in more detail here: https://stackoverflow.com/a/16063711/1641941
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With