Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Post Timeline

use snippet instead of external fiddle
Source Link
Håken Lid
  • 23.2k
  • 10
  • 58
  • 73

Another way that hasn't been mentioned yet is the use of Function.prototype.bind

var funcs = {};
for (var i = 0; i < 3; i++) {
 funcs[i] = function(x) {
 console.log('My value: ' + x);
 }.bind(this, i);
}
for (var j = 0; j < 3; j++) {
 funcs[j]();
}

jsFiddle

var funcs = {};
for (var i = 0; i < 3; i++) {
 funcs[i] = function(x) {
 console.log('My value: ' + x);
 }.bind(this, i);
}
for (var j = 0; j < 3; j++) {
 funcs[j]();
}

UPDATE

As pointed out by @squint and @mekdev, you get better performance by creating the function outside the loop first and then binding the results within the loop.

function log(x) {
 console.log('My value: ' + x);
}
var funcs = [];
for (var i = 0; i < 3; i++) {
 funcs[i] = log.bind(this, i);
}
for (var j = 0; j < 3; j++) {
 funcs[j]();
}

jsFiddle

function log(x) {
 console.log('My value: ' + x);
}
var funcs = [];
for (var i = 0; i < 3; i++) {
 funcs[i] = log.bind(this, i);
}
for (var j = 0; j < 3; j++) {
 funcs[j]();
}

Another way that hasn't been mentioned yet is the use of Function.prototype.bind

var funcs = {};
for (var i = 0; i < 3; i++) {
 funcs[i] = function(x) {
 console.log('My value: ' + x);
 }.bind(this, i);
}
for (var j = 0; j < 3; j++) {
 funcs[j]();
}

jsFiddle

UPDATE

As pointed out by @squint and @mekdev, you get better performance by creating the function outside the loop first and then binding the results within the loop.

function log(x) {
 console.log('My value: ' + x);
}
var funcs = [];
for (var i = 0; i < 3; i++) {
 funcs[i] = log.bind(this, i);
}
for (var j = 0; j < 3; j++) {
 funcs[j]();
}

jsFiddle

Another way that hasn't been mentioned yet is the use of Function.prototype.bind

var funcs = {};
for (var i = 0; i < 3; i++) {
 funcs[i] = function(x) {
 console.log('My value: ' + x);
 }.bind(this, i);
}
for (var j = 0; j < 3; j++) {
 funcs[j]();
}

UPDATE

As pointed out by @squint and @mekdev, you get better performance by creating the function outside the loop first and then binding the results within the loop.

function log(x) {
 console.log('My value: ' + x);
}
var funcs = [];
for (var i = 0; i < 3; i++) {
 funcs[i] = log.bind(this, i);
}
for (var j = 0; j < 3; j++) {
 funcs[j]();
}

increase performance of example
Source Link
Aust
  • 11.6k
  • 13
  • 48
  • 75

Another way that hasn't been mentioned yet is the use of Function.prototype.bind

var funcs = {};
for (var i = 0; i < 3; i++) {
 funcs[i] = function(x) {
 console.log('My value: ' + x);
 }.bind(this, i);
}
for (var j = 0; j < 3; j++) {
 funcs[j]();
}

jsFiddle

UPDATE

As pointed out by @squint and @mekdev, you get better performance by creating the function outside the loop first and then binding the results within the loop.

function log(x) {
 console.log('My value: ' + x);
}
var funcs = [];
for (var i = 0; i < 3; i++) {
 funcs[i] = log.bind(this, i);
}
for (var j = 0; j < 3; j++) {
 funcs[j]();
}

jsFiddle

Another way that hasn't been mentioned yet is the use of Function.prototype.bind

var funcs = {};
for (var i = 0; i < 3; i++) {
 funcs[i] = function(x) {
 console.log('My value: ' + x);
 }.bind(this, i);
}
for (var j = 0; j < 3; j++) {
 funcs[j]();
}

jsFiddle

Another way that hasn't been mentioned yet is the use of Function.prototype.bind

var funcs = {};
for (var i = 0; i < 3; i++) {
 funcs[i] = function(x) {
 console.log('My value: ' + x);
 }.bind(this, i);
}
for (var j = 0; j < 3; j++) {
 funcs[j]();
}

jsFiddle

UPDATE

As pointed out by @squint and @mekdev, you get better performance by creating the function outside the loop first and then binding the results within the loop.

function log(x) {
 console.log('My value: ' + x);
}
var funcs = [];
for (var i = 0; i < 3; i++) {
 funcs[i] = log.bind(this, i);
}
for (var j = 0; j < 3; j++) {
 funcs[j]();
}

jsFiddle

The example explains better when naming the variable x instead of j
Source Link
Aust
  • 11.6k
  • 13
  • 48
  • 75

Another way that hasn't been mentioned yet is the use of Function.prototype.bind

var funcs = {};
for (var i = 0; i < 3; i++) {
 funcs[i] = function(jx) {
 console.log('My value: ' + jx);
 }.bind(this, i);
}
for (var j = 0; j < 3; j++) {
 funcs[j]();
}

jsFiddle jsFiddle

Another way that hasn't been mentioned yet is the use of Function.prototype.bind

var funcs = {};
for (var i = 0; i < 3; i++) {
 funcs[i] = function(j) {
 console.log('My value: ' + j);
 }.bind(this, i);
}
for (var j = 0; j < 3; j++) {
 funcs[j]();
}

jsFiddle

Another way that hasn't been mentioned yet is the use of Function.prototype.bind

var funcs = {};
for (var i = 0; i < 3; i++) {
 funcs[i] = function(x) {
 console.log('My value: ' + x);
 }.bind(this, i);
}
for (var j = 0; j < 3; j++) {
 funcs[j]();
}

jsFiddle

Post Undeleted by Aust
Post Deleted by Aust
Source Link
Aust
  • 11.6k
  • 13
  • 48
  • 75
Loading
lang-js

AltStyle によって変換されたページ (->オリジナル) /