Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var someArray = [ /* whatever */ ];
- // ...
- someArray.forEach(function(arrayElement) {
- // ... code code code for this one element
- someAsynchronousFunction(arrayElement, function() {
- arrayElement.doSomething();
- });
- });
- for (let i = 0; i < 3; i++) {
- funcs[i] = function() {
- console.log("My value: " + i);
- };
- }
- var funcs = [];
- for (var i = 0; i < 3; i++) {
- funcs[i] = (function(index) {
- return function() {
- console.log("My value: " + index);
- };
- }(i));
- }
- for (var j = 0; j < 3; j++) {
- funcs[j]();
- }
- for (var i = 0; i < 3; i++) {
- (function(index) {
- console.log('iterator: ' + index);
- //now you can also loop an ajax call here
- //without losing track of the iterator value: $.ajax({});
- })(i);
- }
- var funcs = [];
- for (let i = 0; i < 3; i++) {
- funcs[i] = function() {
- console.log("My value: " + i);
- };
- }
- var funcs = {};
- for (var i = 0; i < 3; i++) {
- let index = i; //add this
- funcs[i] = function() {
- console.log("My value: " + index); //change to the copy
- };
- }
- for (var j = 0; j < 3; j++) {
- funcs[j]();
- }
- for (var i = 0; i < 3; i++) {
- funcs[i] = (function() {
- var index = i;
- return function() {
- console.log("My value: " + index);
- }
- })();
- }
- function makeCounter()
- {
- var obj = {counter: 0};
- return {
- inc: function(){obj.counter ++;},
- get: function(){return obj.counter;}
- };
- }
- counter1 = makeCounter();
- counter2 = makeCounter();
- counter1.inc();
- alert(counter1.get()); // returns 1
- alert(counter2.get()); // returns 0
- var counters = [];
- function makeCounters(num)
- {
- for (var i = 0; i < num; i++)
- {
- var obj = {counter: 0};
- counters[i] = {
- inc: function(){obj.counter++;},
- get: function(){return obj.counter;}
- };
- }
- }
- makeCounters(2);
- counters[0].inc();
- alert(counters[0].get()); // returns 1
- alert(counters[1].get()); // returns 1
- function makeHelper(obj)
- {
- return {
- inc: function(){obj.counter++;},
- get: function(){return obj.counter;}
- };
- }
- function makeCounters(num)
- {
- for (var i = 0; i < num; i++)
- {
- var obj = {counter: 0};
- counters[i] = makeHelper(obj);
- }
- }
- var funcs = [];
- for(var i =0; i<3; i++){
- funcs[i] = function(){
- alert(i);
- }
- }
- for(var j =0; j<3; j++){
- funcs[j]();
- }
- var funcs = [];
- for(var new_i =0; new_i<3; new_i++){
- (function(i){
- funcs[i] = function(){
- alert(i);
- }
- })(new_i);
- }
- for(var j =0; j<3; j++){
- funcs[j]();
- }
- for (var i = 0; i < 3; i++) {
- createfunc(i)();
- }
- function createfunc(i) {
- return function(){console.log("My value: " + i);};
- }
- funcs[i] = function() { // and store them in funcs
- throw new Error("test");
- console.log("My value: " + i); // each should log its value.
- };
- funcs[i] = new function() {
- var closedVariable = i;
- return function(){
- console.log("My value: " + closedVariable);
- };
- };
- var funcs = [];
- [0,1,2].forEach(function(i) { // let's create 3 functions
- funcs[i] = function() { // and store them in funcs
- console.log("My value: " + i); // each should log its value.
- };
- })
- for (var j = 0; j < 3; j++) {
- funcs[j](); // and now let's run each one to see
- }
- My value: 0
- My value: 1
- My value: 2
- funcs = {};
- for (var i = 0; i < 3; i++) {
- funcs[i] = function inner() { // function inner's scope contains nothing
- console.log("My value: " + i);
- };
- }
- console.log(window.i) // test value 'i', print 3
- funcs = {};
- function outer(i) { // function outer's scope contains 'i'
- return function inner() { // function inner, closure created
- console.log("My value: " + i);
- };
- }
- for (var i = 0; i < 3; i++) {
- funcs[i] = outer(i);
- }
- console.log(window.i) // print 3 still
- var funcs = [];
- for (let i = 0; i < 3; i++) { // let's create 3 functions
- funcs[i] = function() { // and store them in funcs
- console.log("My value: " + i); // each should log its value.
- };
- }
- for (let j = 0; j < 3; j++) {
- funcs[j](); // and now let's run each one to see
- }
- [0,2,3].forEach(function(i){ console.log('My value:', i); });
- // My value: 0
- // My value: 2
- // My value: 3
- var funcs = [];
- for (var i = 0; i < 3; i++) { // let's create 3 functions
- funcs[i] = function() { // and store them in funcs
- console.log("My value: " + i); // each should log its value.
- };
- }
- for (var j = 0; j < 3; j++) {
- funcs[j](); // and now let's run each one to see
- }
- var funcs = [];
- for (var i = 0; i < 3; i++) {
- let index = i;
- funcs[i] = function() {
- console.log("My value: " + index);
- };
- }
- for (var j = 0; j < 3; j++) {
- funcs[j]();
- }
- var funcs = [];
- function tempFunc(i){
- return function(){
- console.log("My value: " + i);
- };
- }
- for (var i = 0; i < 3; i++) {
- funcs[i] = tempFunc(i);
- }
- for (var j = 0; j < 3; j++) {
- funcs[j]();
- }
- <script>
- var funcs = [];
- for (var i = 0; i < 3; i++) {
- funcs[i] = function () {
- debugger;
- console.log("My value: " + i);
- };
- }
- console.log(funcs);
- </script>
- <script>
- var funcs = [];
- for (let i = 0; i < 3; i++) {
- funcs[i] = function () {
- debugger;
- console.log("My value: " + i);
- };
- }
- console.log(funcs);
- </script>
- var funcs = [];
- for (var i = 0; i < 3; i++) {
- (funcs[i] = function() {
- console.log("My value: " + i);
- })(i);
- }
- var funcs = [];
- new Array(3).fill(0).forEach(function (_, i) { // creating a range
- funcs[i] = function() {
- // now i is safely incapsulated
- console.log("My value: " + i);
- };
- });
- for (var j = 0; j < 3; j++) {
- funcs[j](); // 0, 1, 2
- }
- var funcs = [];
- for (var i = 0; i < 3; i++) { // let's create 3 functions
- funcs[i] = function(param) { // and store them in funcs
- console.log("My value: " + param); // each should log its value.
- };
- }
- for (var j = 0; j < 3; j++) {
- funcs[j](j); // and now let's run each one to see with j
- }
- var funcs = Query.range(0,3).each(function(i){
- return function() {
- console.log("My value: " + i);
- };
- });
- funcs.iterate(function(f){ f(); });
- var funcs = [];
- for (var i = 0; i < 3; i++) { // let's create 3 functions
- funcs[i] = curryShowValue(i);
- }
- for (var j = 0; j < 3; j++) {
- funcs[j](); // and now let's run each one to see
- }
- function curryShowValue(i) {
- return function showValue() {
- console.log("My value: " + i);
- }
- }
- Create variable `funcs` and assign it an empty array;
- Loop from 0 up until it is less than 3 and assign it to variable `i`;
- Push to variable `funcs` next function:
- // Only push (save), but don't execute
- **Write to console current value of variable `i`;**
- // First loop has ended, i = 3;
- Loop from 0 up until it is less than 3 and assign it to variable `j`;
- Call `j`-th function from variable `funcs`:
- **Write to console current value of variable `i`;**
- // Ask yourself NOW! What is the value of i?
- var funcs = [];
- for (var i = 0; i < 3; i++) { // let's create 3 functions
- funcs[i] = function(x) { // and store them in funcs
- console.log("My value: " + x); // each should log its value.
- }.bind(null, i);
- }
- for (var j = 0; j < 3; j++) {
- funcs[j](); // and now let's run each one to see
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement