Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. **Classes**
  2.  
  3. ***Functional classes***
  4.  
  5. A javascript class is just a function capable of creating objects comforming to the same interface of methods and properties.
  6.  
  7. ```js
  8. var Car = function(loc){
  9. var obj = {loc: loc};
  10. extend(obj, Car.methods);
  11. return obj;
  12. };
  13.  
  14. Car.methods ={
  15. move:function(){
  16. this.loc++;
  17. }
  18. };
  19.  
  20. ```
  21.  
  22. To call the above class in our programme
  23. ```js
  24. mercedes = Car(1);
  25. mercedes.move();
  26. mazda =Car(2);
  27. mazda.move();
  28. ```
  29.  
  30. ***Prototypal Classes (Prototypal Class Pattern)***
  31. Instead of copying the functions from the ```js Car.methods ``` object using extends, whe can rely on the more effective prototype delegation
  32. look up approach. The code would be as follows
  33.  
  34.  
  35. ```js
  36. var Car = function(loc){
  37. var obj = Object.create(Car.methods);
  38. obj.loc=loc;
  39. return obj;
  40. };
  41.  
  42. Car.methods ={
  43. move:function(){
  44. this.loc++;
  45. }
  46. };
  47.  
  48. ```
  49. This way of creating classes is so common in javascript, that the language supports prototype delegation natively. The reason for this
  50. is the obvious benefit of using delegation rather than prototyping. The language whenever a function is created it will have
  51. 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.
  52.  
  53. The default object that comes object is stored at the key ```j .prototype ``` Applying this ...
  54.  
  55.  
  56. ```js
  57. var Car = function(loc){
  58. var obj = Object.create(Car.prototype);
  59. obj.loc=loc;
  60. return obj;
  61. };
  62.  
  63. Car.prototype.move =function(){
  64. this.loc++;
  65. };
  66.  
  67. ```
  68.  
  69.  
  70. *** Pseudoclassical Class Pattern ***
  71.  
  72. By adding a thin layer of syntactic conveniences, we can make code in the prototypal class above resemble the class pattern in other
  73. languages as follows:
  74.  
  75. ```js
  76. var Car = function(loc){
  77. var obj = Object.create(Car.prototype);
  78. obj.loc=loc;
  79. return obj;
  80. };
  81.  
  82. Car.prototype.move =function(){
  83. this.loc++;
  84. };
  85.  
  86. ```
  87. The lines ```js var obj = Object.create(Car.prototype);``` and ``` return obj;``` look likely to be repeated in every prototypal
  88. class. This can be alleviated using the keyword ```js new ```. Whenever we use this keyword in front of a function invocation our
  89. function is going to run in a special mode called Constructor mode
  90.  
  91. Constructor mode will do most of this work for us automatically. What's constructor mode? Constructor mode is a way for our interpreter
  92. to insert a few lines of operations into code because they will be required whenever instatiating our object. It temporarily makes
  93. a function run as if there was some code at the beginning and the end even though we had never typed that code ourselves.
  94.  
  95. The operations it will insert will do the same work as we have in the prototypal class.
  96.  
  97. eg.
  98. ```js
  99. var volkswagen = new Car(3);
  100. volkswagen.new()
  101. ```
  102. calling the prototypal class above with the new keyword adds these two lines
  103. ```js
  104. this = Object.create(Car.prototype):
  105. return this;
  106. ```
  107. However, these lines don't actually appear in the code. They are just used because of the keyword new in the invocation. If we
  108. decided to be calling the function with the new keyword, then we can refactor the code as
  109.  
  110. ```js
  111. var Car = function(loc){
  112. this.loc=loc;
  113. };
  114.  
  115. Car.prototype.move =function(){
  116. this.loc++;
  117. };
  118.  
  119. ```
  120.  
  121. This is what we'd expect to have if we were writing a pseudoclassical patterned class.
  122. So the structure is really just the same, but thinly veiled syntactic convinience.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement