var funcs = [];
// let's create 3 functions
for (var i = 0; i < 3; i++) {
// and store them in funcs
funcs[i] = function() {
// each should log its value.
console.log("My value: " + i);
};
}
for (var j = 0; j < 3; j++) {
// and now let's run each one to see
funcs[j]();
}Run Code Online (Sandbox Code Playgroud)
它输出这个:
我的价值:3
我的价值:3
我的价值:3
而我希望它输出:
我的价值:0
我的价值:1
我的价值:2
使用事件侦听器导致运行函数的延迟时,会出现同样的问题:
var buttons = document.getElementsByTagName("button");
// let's create 3 …Run Code Online (Sandbox Code Playgroud)为什么这些例子中的第一个不起作用,但所有其他例子都不起作用?
// 1 - does not work
(function() {
setTimeout(someFunction1, 10);
var someFunction1 = function() { alert('here1'); };
})();
// 2
(function() {
setTimeout(someFunction2, 10);
function someFunction2() { alert('here2'); }
})();
// 3
(function() {
setTimeout(function() { someFunction3(); }, 10);
var someFunction3 = function() { alert('here3'); };
})();
// 4
(function() {
setTimeout(function() { someFunction4(); }, 10);
function someFunction4() { alert('here4'); }
})();
Run Code Online (Sandbox Code Playgroud) 我确定我的问题是基于对node.js中的异步编程缺乏了解,但是这里有.
例如:我有一个我想要抓取的链接列表.当每个异步请求返回时,我想知道它是用于哪个URL.但是,大概是因为竞争条件,每个请求返回的URL都设置为列表中的最后一个值.
var links = ['http://google.com', 'http://yahoo.com'];
for (link in links) {
var url = links[link];
require('request')(url, function() {
console.log(url);
});
}
Run Code Online (Sandbox Code Playgroud)
预期产量:
http://google.com
http://yahoo.com
Run Code Online (Sandbox Code Playgroud)
实际产量:
http://yahoo.com
http://yahoo.com
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:
PS:对于1.我不想要一个检查回调参数的解决方案,而是一个回调知道变量'从上面'的一般方法.
代码有一个问题,即在调用异步函数时变量会被写入.怎么修好?
码:
for (x in files) {
asynchronousFunction(var1, var2, function(){
console.log(x.someVaraible);
});
}
Run Code Online (Sandbox Code Playgroud)
现在的问题是,当调用asynchronousFunction中的回调函数时,x.files变量已更新为json数组文件中的下一个变量.我希望变量应该包含以前的值.
回调函数中的变量数不能更改,因此无法在回调函数中发送变量名.
我有以下代码,我不明白为什么我重新声明get方法时无法访问私有属性.
(function(w,d,a,undefined){
var cfg = {
currency: 'GBP',
exponent: 2
};
var get = function () {
return cfg;
};
a.init = function (settings) {
for (var k in settings) {
cfg[k] = settings[k];
}
};
a.set = function (args) {
get = args.get || get;
//eval(args) //works but why??
};
a.get = function () {
return get();
};
})(window,document,window.fxc = {});
fxc.init({currency: 'EUR'});
// prints, Object { currency="EUR", exponent=2}
console.log(fxc.get());
fxc.set({get: function(msg){
// cannot access private properties
return cfg; …Run Code Online (Sandbox Code Playgroud) 我正在按照教程显示工厂模式以在javascript中创建对象.下面的代码让我难以理解它的工作原理.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>6-2.htm</title>
</head>
<body>
<script type="text/javascript">
function createAddress(street, city, state, zip) {
var obj = new Object();
obj.street = street;
obj.city = city;
obj.state = state;
obj.zip = zip;
obj.showLabel = function() {
//alert(this.street + "\n" + this.city + ", " + this.state + " " + this.zip);
//var obj;
alert(obj.street + "\n" + obj.city + ", " + obj.state + " " + obj.zip);
};
return obj;
};
var JohnAddr = …Run Code Online (Sandbox Code Playgroud) 我有这个问题从node.js异步查询mongodb.这是我的代码
var values = [];
var positives = new Array();
var negatives = new Array();
var server1 = new mongodb.Server('localhost',27017, {auto_reconnect: true});
var db1 = new mongodb.Db('clicker', server1);
db1.open(function(err, db) {
if(!err) {
db1.collection('feedback', function(err, collection) {
for (var i=0;i <5; i++) {
collection.find(
{value:1},
{created_on:
{
$gte:startTime + (i*60*1000 - 30*1000),
$lt: startTime + (i*60*1000 + 30*1000)
}
},
function(err_positive, result_positive) {
result_positive.count(function(err, count){
console.log("Total matches: " + count);
positives[i] = count;
});
}
);
collection.find(
{value:0},
{created_on:
{ …Run Code Online (Sandbox Code Playgroud) 我无法将i的值传递给scMotion函数
for ( i = 0; i < tmp.length; i++ ) {
document.getElementById("sc"+i).onmousedown=function() { return scMotion(i,'up') };
}
Run Code Online (Sandbox Code Playgroud)
为了澄清这个问题,这个for循环正在做其他的事情,向dom添加元素.
由于某种原因,即使我在数字39处,在我附加的函数中传递的i的值是i的最终值,即80.
javascript ×7
asynchronous ×2
node.js ×2
closures ×1
loops ×1
mongodb ×1
scoping ×1
web-crawler ×1