Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- **Classes**
- ***Functional classes***
- A javascript class is just a function capable of creating objects comforming to the same interface of methods and properties.
- ```js
- var Car = function(loc){
- var obj = {loc: loc};
- extend(obj, Car.methods);
- return obj;
- };
- Car.methods ={
- move:function(){
- this.loc++;
- }
- };
- ```
- To call the above class in our programme
- ```js
- mercedes = Car(1);
- mercedes.move();
- mazda =Car(2);
- mazda.move();
- ```
- ***Prototypal Classes (Prototypal Class Pattern)***
- Instead of copying the functions from the ```js Car.methods ``` object using extends, whe can rely on the more effective prototype delegation
- look up approach. The code would be as follows
- ```js
- var Car = function(loc){
- var obj = Object.create(Car.methods);
- obj.loc=loc;
- return obj;
- };
- Car.methods ={
- move:function(){
- this.loc++;
- }
- };
- ```
- This way of creating classes is so common in javascript, that the language supports prototype delegation natively. The reason for this
- is the obvious benefit of using delegation rather than prototyping. The language whenever a function is created it will have
- an object attached to it as a container for methods just in case you plan to use that function to build instances of a class.
- The default object that comes object is stored at the key ```j .prototype ``` Applying this ...
- ```js
- var Car = function(loc){
- var obj = Object.create(Car.prototype);
- obj.loc=loc;
- return obj;
- };
- Car.prototype.move =function(){
- this.loc++;
- };
- ```
- *** Pseudoclassical Class Pattern ***
- By adding a thin layer of syntactic conveniences, we can make code in the prototypal class above resemble the class pattern in other
- languages as follows:
- ```js
- var Car = function(loc){
- var obj = Object.create(Car.prototype);
- obj.loc=loc;
- return obj;
- };
- Car.prototype.move =function(){
- this.loc++;
- };
- ```
- The lines ```js var obj = Object.create(Car.prototype);``` and ``` return obj;``` look likely to be repeated in every prototypal
- class. This can be alleviated using the keyword ```js new ```. Whenever we use this keyword in front of a function invocation our
- function is going to run in a special mode called Constructor mode
- Constructor mode will do most of this work for us automatically. What's constructor mode? Constructor mode is a way for our interpreter
- to insert a few lines of operations into code because they will be required whenever instatiating our object. It temporarily makes
- a function run as if there was some code at the beginning and the end even though we had never typed that code ourselves.
- The operations it will insert will do the same work as we have in the prototypal class.
- eg.
- ```js
- var volkswagen = new Car(3);
- volkswagen.new()
- ```
- calling the prototypal class above with the new keyword adds these two lines
- ```js
- this = Object.create(Car.prototype):
- return this;
- ```
- However, these lines don't actually appear in the code. They are just used because of the keyword new in the invocation. If we
- decided to be calling the function with the new keyword, then we can refactor the code as
- ```js
- var Car = function(loc){
- this.loc=loc;
- };
- Car.prototype.move =function(){
- this.loc++;
- };
- ```
- This is what we'd expect to have if we were writing a pseudoclassical patterned class.
- So the structure is really just the same, but thinly veiled syntactic convinience.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement