Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.90 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. for (var i = 0; i < 3; i++) {
  30.  
  31. (function(index) {
  32. console.log('iterator: ' + index);
  33. //now you can also loop an ajax call here
  34. //without losing track of the iterator value: $.ajax({});
  35. })(i);
  36.  
  37. }
  38.  
  39. var funcs = [];
  40. for (let i = 0; i < 3; i++) {
  41. funcs[i] = function() {
  42. console.log("My value: " + i);
  43. };
  44. }
  45.  
  46. var funcs = {};
  47. for (var i = 0; i < 3; i++) {
  48. let index = i; //add this
  49. funcs[i] = function() {
  50. console.log("My value: " + index); //change to the copy
  51. };
  52. }
  53. for (var j = 0; j < 3; j++) {
  54. funcs[j]();
  55. }
  56.  
  57. for (var i = 0; i < 3; i++) {
  58. funcs[i] = (function() {
  59. var index = i;
  60. return function() {
  61. console.log("My value: " + index);
  62. }
  63. })();
  64. }
  65.  
  66. function makeCounter()
  67. {
  68. var obj = {counter: 0};
  69. return {
  70. inc: function(){obj.counter ++;},
  71. get: function(){return obj.counter;}
  72. };
  73. }
  74.  
  75. counter1 = makeCounter();
  76. counter2 = makeCounter();
  77.  
  78. counter1.inc();
  79.  
  80. alert(counter1.get()); // returns 1
  81. alert(counter2.get()); // returns 0
  82.  
  83. var counters = [];
  84.  
  85. function makeCounters(num)
  86. {
  87. for (var i = 0; i < num; i++)
  88. {
  89. var obj = {counter: 0};
  90. counters[i] = {
  91. inc: function(){obj.counter++;},
  92. get: function(){return obj.counter;}
  93. };
  94. }
  95. }
  96.  
  97. makeCounters(2);
  98.  
  99. counters[0].inc();
  100.  
  101. alert(counters[0].get()); // returns 1
  102. alert(counters[1].get()); // returns 1
  103.  
  104. function makeHelper(obj)
  105. {
  106. return {
  107. inc: function(){obj.counter++;},
  108. get: function(){return obj.counter;}
  109. };
  110. }
  111.  
  112. function makeCounters(num)
  113. {
  114. for (var i = 0; i < num; i++)
  115. {
  116. var obj = {counter: 0};
  117. counters[i] = makeHelper(obj);
  118. }
  119. }
  120.  
  121. var funcs = [];
  122. for(var i =0; i<3; i++){
  123. funcs[i] = function(){
  124. alert(i);
  125. }
  126. }
  127.  
  128. for(var j =0; j<3; j++){
  129. funcs[j]();
  130. }
  131.  
  132. var funcs = [];
  133. for(var new_i =0; new_i<3; new_i++){
  134. (function(i){
  135. funcs[i] = function(){
  136. alert(i);
  137. }
  138. })(new_i);
  139. }
  140.  
  141. for(var j =0; j<3; j++){
  142. funcs[j]();
  143. }
  144.  
  145. for (var i = 0; i < 3; i++) {
  146. createfunc(i)();
  147. }
  148.  
  149. function createfunc(i) {
  150. return function(){console.log("My value: " + i);};
  151. }
  152.  
  153. funcs[i] = function() { // and store them in funcs
  154. throw new Error("test");
  155. console.log("My value: " + i); // each should log its value.
  156. };
  157.  
  158. funcs[i] = new function() {
  159. var closedVariable = i;
  160. return function(){
  161. console.log("My value: " + closedVariable);
  162. };
  163. };
  164.  
  165. var funcs = [];
  166. [0,1,2].forEach(function(i) { // let's create 3 functions
  167. funcs[i] = function() { // and store them in funcs
  168. console.log("My value: " + i); // each should log its value.
  169. };
  170. })
  171. for (var j = 0; j < 3; j++) {
  172. funcs[j](); // and now let's run each one to see
  173. }
  174.  
  175. My value: 0
  176. My value: 1
  177. My value: 2
  178.  
  179. funcs = {};
  180. for (var i = 0; i < 3; i++) {
  181. funcs[i] = function inner() { // function inner's scope contains nothing
  182. console.log("My value: " + i);
  183. };
  184. }
  185. console.log(window.i) // test value 'i', print 3
  186.  
  187. funcs = {};
  188. function outer(i) { // function outer's scope contains 'i'
  189. return function inner() { // function inner, closure created
  190. console.log("My value: " + i);
  191. };
  192. }
  193. for (var i = 0; i < 3; i++) {
  194. funcs[i] = outer(i);
  195. }
  196. console.log(window.i) // print 3 still
  197.  
  198. var funcs = [];
  199. for (let i = 0; i < 3; i++) { // let's create 3 functions
  200. funcs[i] = function() { // and store them in funcs
  201. console.log("My value: " + i); // each should log its value.
  202. };
  203. }
  204. for (let j = 0; j < 3; j++) {
  205. funcs[j](); // and now let's run each one to see
  206. }
  207.  
  208. [0,2,3].forEach(function(i){ console.log('My value:', i); });
  209. // My value: 0
  210. // My value: 2
  211. // My value: 3
  212.  
  213. var funcs = [];
  214. for (var i = 0; i < 3; i++) { // let's create 3 functions
  215. funcs[i] = function() { // and store them in funcs
  216. console.log("My value: " + i); // each should log its value.
  217. };
  218. }
  219. for (var j = 0; j < 3; j++) {
  220. funcs[j](); // and now let's run each one to see
  221. }
  222.  
  223. var funcs = [];
  224. for (var i = 0; i < 3; i++) {
  225. let index = i;
  226. funcs[i] = function() {
  227. console.log("My value: " + index);
  228. };
  229. }
  230. for (var j = 0; j < 3; j++) {
  231. funcs[j]();
  232. }
  233.  
  234. var funcs = [];
  235. function tempFunc(i){
  236. return function(){
  237. console.log("My value: " + i);
  238. };
  239. }
  240. for (var i = 0; i < 3; i++) {
  241. funcs[i] = tempFunc(i);
  242. }
  243. for (var j = 0; j < 3; j++) {
  244. funcs[j]();
  245. }
  246.  
  247. <script>
  248. var funcs = [];
  249. for (var i = 0; i < 3; i++) {
  250. funcs[i] = function () {
  251. debugger;
  252. console.log("My value: " + i);
  253. };
  254. }
  255. console.log(funcs);
  256. </script>
  257.  
  258. <script>
  259. var funcs = [];
  260. for (let i = 0; i < 3; i++) {
  261. funcs[i] = function () {
  262. debugger;
  263. console.log("My value: " + i);
  264. };
  265. }
  266. console.log(funcs);
  267. </script>
  268.  
  269. var funcs = [];
  270. for (var i = 0; i < 3; i++) {
  271. (funcs[i] = function() {
  272. console.log("My value: " + i);
  273. })(i);
  274. }
  275.  
  276. var funcs = [];
  277.  
  278. new Array(3).fill(0).forEach(function (_, i) { // creating a range
  279. funcs[i] = function() {
  280. // now i is safely incapsulated
  281. console.log("My value: " + i);
  282. };
  283. });
  284.  
  285. for (var j = 0; j < 3; j++) {
  286. funcs[j](); // 0, 1, 2
  287. }
  288.  
  289. var funcs = [];
  290. for (var i = 0; i < 3; i++) { // let's create 3 functions
  291. funcs[i] = function(param) { // and store them in funcs
  292. console.log("My value: " + param); // each should log its value.
  293. };
  294. }
  295. for (var j = 0; j < 3; j++) {
  296. funcs[j](j); // and now let's run each one to see with j
  297. }
  298.  
  299. var funcs = Query.range(0,3).each(function(i){
  300. return function() {
  301. console.log("My value: " + i);
  302. };
  303. });
  304.  
  305. funcs.iterate(function(f){ f(); });
  306.  
  307. var funcs = [];
  308. for (var i = 0; i < 3; i++) { // let's create 3 functions
  309. funcs[i] = curryShowValue(i);
  310. }
  311. for (var j = 0; j < 3; j++) {
  312. funcs[j](); // and now let's run each one to see
  313. }
  314.  
  315. function curryShowValue(i) {
  316. return function showValue() {
  317. console.log("My value: " + i);
  318. }
  319. }
  320.  
  321. Create variable `funcs` and assign it an empty array;
  322. Loop from 0 up until it is less than 3 and assign it to variable `i`;
  323. Push to variable `funcs` next function:
  324. // Only push (save), but don't execute
  325. **Write to console current value of variable `i`;**
  326.  
  327. // First loop has ended, i = 3;
  328.  
  329. Loop from 0 up until it is less than 3 and assign it to variable `j`;
  330. Call `j`-th function from variable `funcs`:
  331. **Write to console current value of variable `i`;**
  332. // Ask yourself NOW! What is the value of i?
  333.  
  334. var funcs = [];
  335. for (var i = 0; i < 3; i++) { // let's create 3 functions
  336. funcs[i] = function(x) { // and store them in funcs
  337. console.log("My value: " + x); // each should log its value.
  338. }.bind(null, i);
  339. }
  340. for (var j = 0; j < 3; j++) {
  341. funcs[j](); // and now let's run each one to see
  342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement