Guest User

Untitled

a guest
Jan 4th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.73 KB | None | 0 0
  1. var someArray = [ /* whatever */ ];
  2. // ...
  3. someArray.forEach(function(arrayElement) {
  4. // ... code code code for this one element
  5. someAsynchronousFunction(arrayElement, function() {
  6. arrayElement.doSomething();
  7. });
  8. });
  9.  
  10. for (let i = 0; i < 3; i++) {
  11. funcs[i] = function() {
  12. console.log("My value: " + i);
  13. };
  14. }
  15.  
  16. var funcs = [];
  17.  
  18. for (var i = 0; i < 3; i++) {
  19. funcs[i] = (function(index) {
  20. return function() {
  21. console.log("My value: " + index);
  22. };
  23. }(i));
  24. }
  25. for (var j = 0; j < 3; j++) {
  26. funcs[j]();
  27. }
  28.  
  29. var funcs = {};
  30. for (var i = 0; i < 3; i++) {
  31. funcs[i] = function(x) {
  32. console.log('My value: ' + x);
  33. }.bind(this, i);
  34. }
  35. for (var j = 0; j < 3; j++) {
  36. funcs[j]();
  37. }
  38.  
  39. function log(x) {
  40. console.log('My value: ' + x);
  41. }
  42.  
  43. var funcs = [];
  44.  
  45. for (var i = 0; i < 3; i++) {
  46. funcs[i] = log.bind(this, i);
  47. }
  48.  
  49. for (var j = 0; j < 3; j++) {
  50. funcs[j]();
  51. }
  52.  
  53. for (var i = 0; i < 3; i++) {
  54.  
  55. (function(index) {
  56. console.log('iterator: ' + index);
  57. //now you can also loop an ajax call here
  58. //without losing track of the iterator value: $.ajax({});
  59. })(i);
  60.  
  61. }
  62.  
  63. var funcs = [];
  64. for (let i = 0; i < 3; i++) {
  65. funcs[i] = function() {
  66. console.log("My value: " + i);
  67. };
  68. }
  69.  
  70. var funcs = {};
  71. for (var i = 0; i < 3; i++) {
  72. let index = i; //add this
  73. funcs[i] = function() {
  74. console.log("My value: " + index); //change to the copy
  75. };
  76. }
  77. for (var j = 0; j < 3; j++) {
  78. funcs[j]();
  79. }
  80.  
  81. for (var i = 0; i < 3; i++) {
  82. funcs[i] = (function() {
  83. var index = i;
  84. return function() {
  85. console.log("My value: " + index);
  86. }
  87. })();
  88. }
  89.  
  90. function makeCounter()
  91. {
  92. var obj = {counter: 0};
  93. return {
  94. inc: function(){obj.counter ++;},
  95. get: function(){return obj.counter;}
  96. };
  97. }
  98.  
  99. counter1 = makeCounter();
  100. counter2 = makeCounter();
  101.  
  102. counter1.inc();
  103.  
  104. alert(counter1.get()); // returns 1
  105. alert(counter2.get()); // returns 0
  106.  
  107. var counters = [];
  108.  
  109. function makeCounters(num)
  110. {
  111. for (var i = 0; i < num; i++)
  112. {
  113. var obj = {counter: 0};
  114. counters[i] = {
  115. inc: function(){obj.counter++;},
  116. get: function(){return obj.counter;}
  117. };
  118. }
  119. }
  120.  
  121. makeCounters(2);
  122.  
  123. counters[0].inc();
  124.  
  125. alert(counters[0].get()); // returns 1
  126. alert(counters[1].get()); // returns 1
  127.  
  128. function makeHelper(obj)
  129. {
  130. return {
  131. inc: function(){obj.counter++;},
  132. get: function(){return obj.counter;}
  133. };
  134. }
  135.  
  136. function makeCounters(num)
  137. {
  138. for (var i = 0; i < num; i++)
  139. {
  140. var obj = {counter: 0};
  141. counters[i] = makeHelper(obj);
  142. }
  143. }
  144.  
  145. var funcs = [];
  146. for(var i =0; i<3; i++){
  147. funcs[i] = function(){
  148. alert(i);
  149. }
  150. }
  151.  
  152. for(var j =0; j<3; j++){
  153. funcs[j]();
  154. }
  155.  
  156. var funcs = [];
  157. for(var new_i =0; new_i<3; new_i++){
  158. (function(i){
  159. funcs[i] = function(){
  160. alert(i);
  161. }
  162. })(new_i);
  163. }
  164.  
  165. for(var j =0; j<3; j++){
  166. funcs[j]();
  167. }
  168.  
  169. for (var i = 0; i < 3; i++) {
  170. createfunc(i)();
  171. }
  172.  
  173. function createfunc(i) {
  174. return function(){console.log("My value: " + i);};
  175. }
  176.  
  177. funcs[i] = function() { // and store them in funcs
  178. throw new Error("test");
  179. console.log("My value: " + i); // each should log its value.
  180. };
  181.  
  182. funcs[i] = new function() {
  183. var closedVariable = i;
  184. return function(){
  185. console.log("My value: " + closedVariable);
  186. };
  187. };
  188.  
  189. var funcs = {};
  190. [0,1,2].forEach(function(i) { // let's create 3 functions
  191. funcs[i] = function() { // and store them in funcs
  192. console.log("My value: " + i); // each should log its value.
  193. };
  194. })
  195. for (var j = 0; j < 3; j++) {
  196. funcs[j](); // and now let's run each one to see
  197. }
  198.  
  199. My value: 0
  200. My value: 1
  201. My value: 2
  202.  
  203. funcs = {};
  204. for (var i = 0; i < 3; i++) {
  205. funcs[i] = function inner() { // function inner's scope contains nothing
  206. console.log("My value: " + i);
  207. };
  208. }
  209. console.log(window.i) // test value 'i', print 3
  210.  
  211. funcs = {};
  212. function outer(i) { // function outer's scope contains 'i'
  213. return function inner() { // function inner, closure created
  214. console.log("My value: " + i);
  215. };
  216. }
  217. for (var i = 0; i < 3; i++) {
  218. funcs[i] = outer(i);
  219. }
  220. console.log(window.i) // print 3 still
  221.  
  222. var funcs = [];
  223. for (let i = 0; i < 3; i++) { // let's create 3 functions
  224. funcs[i] = function() { // and store them in funcs
  225. console.log("My value: " + i); // each should log its value.
  226. };
  227. }
  228. for (let j = 0; j < 3; j++) {
  229. funcs[j](); // and now let's run each one to see
  230. }
  231.  
  232. [0,2,3].forEach(function(i){ console.log('My value:', i); });
  233. // My value: 0
  234. // My value: 2
  235. // My value: 3
  236.  
  237. var funcs = [];
  238. for (var i = 0; i < 3; i++) { // let's create 3 functions
  239. funcs[i] = function() { // and store them in funcs
  240. console.log("My value: " + i); // each should log its value.
  241. };
  242. }
  243. for (var j = 0; j < 3; j++) {
  244. funcs[j](); // and now let's run each one to see
  245. }
  246.  
  247. var funcs = [];
  248. for (var i = 0; i < 3; i++) {
  249. let index = i;
  250. funcs[i] = function() {
  251. console.log("My value: " + index);
  252. };
  253. }
  254. for (var j = 0; j < 3; j++) {
  255. funcs[j]();
  256. }
  257.  
  258. var funcs = [];
  259. function tempFunc(i){
  260. return function(){
  261. console.log("My value: " + i);
  262. };
  263. }
  264. for (var i = 0; i < 3; i++) {
  265. funcs[i] = tempFunc(i);
  266. }
  267. for (var j = 0; j < 3; j++) {
  268. funcs[j]();
  269. }
  270.  
  271. var funcs = [];
  272.  
  273. new Array(3).fill(0).forEach(function (_, i) { // creating a range
  274. funcs[i] = function() {
  275. // now i is safely incapsulated
  276. console.log("My value: " + i);
  277. };
  278. });
  279.  
  280. for (var j = 0; j < 3; j++) {
  281. funcs[j](); // 0, 1, 2
  282. }
  283.  
  284. var funcs = Query.range(0,3).each(function(i){
  285. return function() {
  286. console.log("My value: " + i);
  287. };
  288. });
  289.  
  290. funcs.iterate(function(f){ f(); });
  291.  
  292. var funcs = [];
  293. for (var i = 0; i < 3; i++) { // let's create 3 functions
  294. funcs[i] = curryShowValue(i);
  295. }
  296. for (var j = 0; j < 3; j++) {
  297. funcs[j](); // and now let's run each one to see
  298. }
  299.  
  300. function curryShowValue(i) {
  301. return function showValue() {
  302. console.log("My value: " + i);
  303. }
  304. }
  305.  
  306. var funcs = [];
  307. for (var i = 0; i < 3; i++) {
  308. (funcs[i] = function() {
  309. console.log("My value: " + i);
  310. })(i);
  311. }
  312.  
  313. Create variable `funcs` and assign it an empty array;
  314. Loop from 0 up until it is less than 3 and assign it to variable `i`;
  315. Push to variable `funcs` next function:
  316. // Only push (save), but don't execute
  317. **Write to console current value of variable `i`;**
  318.  
  319. // First loop has ended, i = 3;
  320.  
  321. Loop from 0 up until it is less than 3 and assign it to variable `j`;
  322. Call `j`-th function from variable `funcs`:
  323. **Write to console current value of variable `i`;**
  324. // Ask yourself NOW! What is the value of i?
  325.  
  326. var funcs = [];
  327. for (var i = 0; i < 3; i++) { // let's create 3 functions
  328. funcs[i] = function(x) { // and store them in funcs
  329. console.log("My value: " + x); // each should log its value.
  330. }.bind(null, i);
  331. }
  332. for (var j = 0; j < 3; j++) {
  333. funcs[j](); // and now let's run each one to see
  334. }
  335.  
  336. // ****************************
  337. // COUNTER BEING A PRIMITIVE
  338. // ****************************
  339. function test1() {
  340. for (var i=0; i<2; i++) {
  341. setTimeout(function() {
  342. console.log(i);
  343. });
  344. }
  345. }
  346. test1();
  347. // 2
  348. // 2
  349.  
  350. function test2() {
  351. function sendRequest(i) {
  352. setTimeout(function() {
  353. console.log(i);
  354. });
  355. }
  356.  
  357. for (var i = 0; i < 2; i++) {
  358. sendRequest(i);
  359. }
  360. }
  361. test2();
  362. // 1
  363. // 2
  364.  
  365. // ****************************
  366. // COUNTER BEING AN OBJECT
  367. // ****************************
  368. function test3() {
  369. var index = { i: 0 };
  370. for (index.i=0; index.i<2; index.i++) {
  371. setTimeout(function() {
  372. console.log('test3: ' + index.i);
  373. });
  374. }
  375. }
  376. test3();
  377. // 2
  378. // 2
  379.  
  380. function test4() {
  381. var index = { i: 0 };
  382. function sendRequest(index, i) {
  383. setTimeout(function() {
  384. console.log('index: ' + index);
  385. console.log('i: ' + i);
  386. console.log(index[i]);
  387. });
  388. }
  389.  
  390. for (index.i=0; index.i<2; index.i++) {
  391. sendRequest(index, index.i);
  392. }
  393. }
  394. test4();
  395. // index: { i: 2}
  396. // 0
  397. // undefined
  398.  
  399. // index: { i: 2}
  400. // 1
  401. // undefined
  402.  
  403. var funcs = [];
  404. for (var i = 0; i < 3; i++) {
  405. var functionBody = 'console.log("My value: ' + i + '");';
  406. funcs[i] = new Function(functionBody);
  407. }
  408.  
  409. for (var j = 0; j < 3; j++) {
  410. funcs[j]();
  411. }
Add Comment
Please, Sign In to add comment