Guest User

Untitled

a guest
Oct 26th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. ## What are the 4 things that happen when we use the `new` in front of a function call?
  2.  
  3. 1. It creates a brand new object.
  4. 2. The new object gets linked to the function's prototype.
  5. 3. The new object gets passed into the function call as `this`.
  6. 4. The function call returns `this`.
  7.  
  8. ## What is a constructor call?
  9.  
  10. It is a function call with the `new` keyword in front of it.
  11.  
  12. ## What does `Object.create` do?
  13.  
  14. It performs the first two actions that using the `new` keyword does:
  15.  
  16. 1. It creates a brand new object.
  17. 2. The new object gets linked to another object.
  18.  
  19. ## What is [[Prototype]] and where does it comes from?
  20.  
  21. It's a linkage from one object to another and it's created at the time the
  22. object is created. (When an object is created it gets linked somewhere.)
  23.  
  24. ## How does [[Prototype]] affect the behavior of an object?
  25.  
  26. If we call methods or properties on an object and they don't exist, the prototype
  27. will attempt to look higher up the prototype chain for the desired behavior.
  28.  
  29. ## What are three ways we find out where an object's [[Prototype]] points to?
  30.  
  31. `foo.__proto__`
  32. `Object.getPrototypeOf`
  33. `foo.constructor.prototype`
  34.  
  35. ## What is a tradeoff of using Prototypes over the module pattern?
  36.  
  37. You lose encapsulation because each property and method is public.
  38.  
  39. ## Why would we use the prototype pattern?
  40.  
  41. We love classes, and the prototype pattern seems to be the best way we have
  42. in Javascript to emulate classes.
  43.  
  44. ## What's a problem of mixing the module pattern and prototypes?
  45.  
  46. The prototype system and the lexical system do not cross over.
  47.  
  48. ## What's the deal with ES6 Classes?
  49.  
  50. The syntax is beautiful!
  51.  
  52. But it's just syntactic sugar over the prototype system. They're designed to pretend
  53. there is a copy operation going on because that's they way classes work in other
  54. languages.
  55.  
  56. ## What's an example of the OLOO pattern?
  57.  
  58. The power of javascript is to be able to create objects, link them together, and
  59. delegate between them.
  60.  
  61. ```javascript
  62. var Foo = {
  63. init: function(who) {
  64. this.me = who;
  65. },
  66. identify: function() {
  67. return `I am ${this.me}`;
  68. }
  69. };
  70.  
  71. var Bar = Object.create(Foo);
  72.  
  73. Bar.speak = function() {
  74. console.log(`Hello, ${this.identity()}!`);
  75. }
  76.  
  77. var b1 = Object.create(Bar);
  78. b1.init('Sweet');
  79. b1.speak();
  80. ```
  81.  
  82. ## How would you write your own `Object.create`?
  83.  
  84. ```javascript
  85. if (!Object.create) {
  86. Object.create = function(o) {
  87. return {
  88. __proto__: o
  89. };
  90. };
  91. }
  92.  
  93. if (!Object.create) {
  94. Object.create = function(o) {
  95. function F() {};
  96. F.prototype = o;
  97. return new F();
  98. };
  99. }
  100. ```
  101.  
  102.  
  103. ## What is virtual composition?
  104.  
  105. It's a pattern where we decide to delegate objects to one another at the call site.
  106.  
  107. Each piece of code is independent. This makes testing MUCH easier.
  108.  
  109. Here's an example:
  110.  
  111. ```javascript
  112. var AuthController = {
  113. authenticate() {
  114. server.authenticate(
  115. [this.username, this.password],
  116. this.handleResponse.bind(this)
  117. );
  118. },
  119. handleResponse(resp) {
  120. if (!resp.ok) this.displayError(resp.msg);
  121. }
  122. };
  123.  
  124. var LoginFormController =
  125. delegateTo(AuthController, {
  126. onSubmit() {
  127. this.username = document.querySelector("#username").textContent;
  128. this.password = document.querySelector("#password").textContent;
  129. this.authenticate();
  130. },
  131. displayError(msg) {
  132. alert(msg)
  133. }
  134. });
  135.  
  136. function delegateTo(delegate, body) {
  137. if (body === undefined) body = {};
  138. return Object.assign(Object.create(delegate), body);
  139. }
  140. ```
  141.  
  142. ## How is javascripts [[Prototype]] chain not like traditional/classical inheritance?
  143.  
  144. The prototype chain provides a live link up the chain instead of copying objects
  145. down the chain.
  146.  
  147. ## What does [[Prototype]] delegation mean and how does it describe object linking in javascript?
  148.  
  149. Delegate is peer-to-peer rather than parent-child. We don't inherit behavior, but
  150. we collaborate with other objects that can provide the behavior we're looking for.
  151.  
  152. Delegation allows two objects to share context at call time. (Virtual composition.)
  153.  
  154. ## What are the benefits of the "behavior delegation" design pattern?
  155.  
  156. Since objects are composed together, it is much easier to test objects in isolation.
  157.  
  158. The code is more explicit.
  159.  
  160. ## What are the tradeoffs of the "behavior delegation" design pattern?
  161.  
  162. We lost encapsulation that we had in the module pattern.
  163.  
  164. The code can be a little more complex.
Add Comment
Please, Sign In to add comment