Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.72 KB | None | 0 0
  1. function MyConstructor(data, transport) {
  2. this.data = data;
  3. transport.on('data', this.alert);
  4. }
  5.  
  6. MyConstructor.prototype.alert = function() {
  7. alert(this.name);
  8. };
  9.  
  10. function foo() {
  11. console.log(this);
  12. }
  13.  
  14. // normal function call
  15. foo(); // `this` will refer to `window`
  16.  
  17. // as object method
  18. var obj = {bar: foo};
  19. obj.bar(); // `this` will refer to `obj`
  20.  
  21. // as constructor function
  22. new foo(); // `this` will refer to an object that inherits from `foo.prototype`
  23.  
  24. function MyConstructor(data, transport) {
  25. this.data = data;
  26. var self = this;
  27. transport.on('data', function() {
  28. alert(self.data);
  29. });
  30. }
  31.  
  32. function MyConstructor(data, transport) {
  33. this.data = data;
  34. var boundFunction = (function() { // parenthesis are not necessary
  35. alert(this.data); // but might improve readability
  36. }).bind(this); // <- here we are calling `.bind()`
  37. transport.on('data', boundFunction);
  38. }
  39.  
  40. function MyConstructor(data, transport) {
  41. this.data = data;
  42. transport.on('data', () => alert(this.data));
  43. }
  44.  
  45. array.map(callback[, thisArg])
  46.  
  47. var arr = [1, 2, 3];
  48. var obj = {multiplier: 42};
  49.  
  50. var new_arr = arr.map(function(v) {
  51. return v * this.multiplier;
  52. }, obj); // <- here we are passing `obj` as second argument
  53.  
  54. function Foo() {
  55. this.data = 42,
  56. document.body.onclick = this.method;
  57. }
  58.  
  59. Foo.prototype.method = function() {
  60. console.log(this.data);
  61. };
  62.  
  63. function method() {
  64. console.log(this.data);
  65. }
  66.  
  67.  
  68. function Foo() {
  69. this.data = 42,
  70. document.body.onclick = this.method;
  71. }
  72.  
  73. Foo.prototype.method = method;
  74.  
  75. document.body.onclick = this.method.bind(this);
  76.  
  77. var self = this;
  78. document.body.onclick = function() {
  79. self.method();
  80. };
  81.  
  82. document.body.onclick = () => this.method();
  83.  
  84. transport.on('data', _.bind(function () {
  85. alert(this.data);
  86. }, this));
  87.  
  88. object.property();
  89.  
  90. var f = object.property;
  91. f();
  92.  
  93. this.saveNextLevelData(this.setAll);
  94.  
  95. this.saveNextLevelData(this.setAll.bind(this));
  96.  
  97. this.saveNextLevelData($.proxy(this.setAll, this));
  98.  
  99. there are two types of scope in javascript. They are :
  100.  
  101. 1) Global Scope
  102.  
  103. 2) Function Scope
  104.  
  105. --------------------------------------------------------------------------------
  106. - -
  107. - Global Scope -
  108. - ( globally "this" refers to window object) -
  109. - -
  110. - function outer_function(callback){ -
  111. - -
  112. - // outer function scope -
  113. - // inside outer function"this" keyword refers to window object - -
  114. - callback() // "this" inside callback also refers window object -
  115.  
  116. - } -
  117. - -
  118. - function callback_function(){ -
  119. - -
  120. - // function to be passed as callback -
  121. - -
  122. - // here "THIS" refers to window object also -
  123. - -
  124. - } -
  125. - -
  126. - outer_function(callback_function) -
  127. - // invoke with callback -
  128. --------------------------------------------------------------------------------
  129.  
  130. function Person(name){
  131.  
  132. this.name = name
  133.  
  134. this.sayNameVersion1 = function(callback){
  135. callback.bind(this)()
  136. }
  137. this.sayNameVersion2 = function(callback){
  138. callback()
  139. }
  140.  
  141. this.sayNameVersion3 = function(callback){
  142. callback.call(this)
  143. }
  144.  
  145. this.sayNameVersion4 = function(callback){
  146. callback.apply(this)
  147. }
  148.  
  149. }
  150.  
  151. function niceCallback(){
  152.  
  153. // function to be used as callback
  154.  
  155. var parentObject = this
  156.  
  157. console.log(parentObject)
  158.  
  159. }
  160.  
  161. var p1 = new Person('zami') // create an instance of Person constructor
  162.  
  163. this.sayNameVersion1 = function(callback){
  164. callback.bind(this)()
  165. }
  166. this.sayNameVersion2 = function(callback){
  167. callback()
  168. }
  169.  
  170. p1.sayNameVersion1(niceCallback) // pass simply the callback and bind happens inside the sayNameVersion1 method
  171.  
  172. p1.sayNameVersion2(niceCallback.bind(p1)) // uses bind before passing callback
  173.  
  174. this.sayNameVersion3 = function(callback){
  175. callback.call(this)
  176. }
  177.  
  178. p1.sayNameVersion3(niceCallback)
  179.  
  180. this.sayNameVersion4 = function(callback){
  181. callback.apply(this)
  182. }
  183.  
  184. p1.sayNameVersion4(niceCallback)
  185.  
  186. setTimeout(function(){
  187. this.methodName();
  188. }.bind(this), 2000);
  189.  
  190. function MyConstructor(data, transport) {
  191. this.data = data;
  192. var self = this;
  193. transport.on('data', function () {
  194. alert(self.data);
  195. });
  196. }
  197.  
  198. function MyConstructor(data, transport) {
  199. this.data = data;
  200. transport.on('data',()=> {
  201. alert(this.data);
  202. });
  203. }
  204.  
  205. function MyConstructor(data, transport) {
  206. this.data = data;
  207. transport.on('data',(function() {
  208. alert(this.data);
  209. }).bind(this);
  210.  
  211. var obj = {
  212. handleEvent(e) {
  213. // always true
  214. console.log(this === obj);
  215. }
  216. };
  217.  
  218. document.body.addEventListener('click', obj);
  219.  
  220. class someView {
  221. onSomeInputKeyUp = (event) => {
  222. console.log(this); // this refers to correct value
  223. // ....
  224. someInitMethod() {
  225. //...
  226. someInput.addEventListener('input', this.onSomeInputKeyUp)
  227.  
  228. function foo(){
  229. var a =2 ;
  230. this.bar();
  231. }
  232.  
  233. function bar (){
  234. console.log(this.a);
  235. }
  236.  
  237. foo(); //undefined, as function call refer to global scope because it’s the invocation context of the function (window.foo()
  238.  
  239. var myFunction = function() {
  240. console.log(this);
  241. }
  242.  
  243. myFunction(); // Window
  244.  
  245. var myFunction = function() {
  246. console.log(this.a);
  247. }
  248.  
  249. var a = 5 ;
  250. myFunction(); //5
  251.  
  252. function MyConstructor(data, transport) {
  253. this.data = data;
  254. transport.on('data', () => {
  255. alert(this.data);
  256. });
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement