Javascript 函数范围/执行顺序问题
Javascript function scope / order of execution issue
我对 javascript 范围/执行顺序有疑问。我在一个函数中创建了一个空对象。然后我在子函数中添加一些属性。但是,父函数中的属性没有改变。
$scope.finder = function (obj) {
var id = obj.oid;
var crit = MC.Criteria('_ownerUserOid == ?', [id]);
theResult = {}; // Scope should be finder function.
database.findOne(crit) // This is a Monaca method for finding from database
.done(function(result) {
// Returns and empty object as expected.
console.log(JSON.stringify(theResult));
theResult = result;
// Returns the search result object as again expected.
console.log(JSON.stringify(theResult));
});
// Here's the issue - when I log and return theResult, I get an empty object again.
// I think it has to do something with the scope.
// Also, this logs to console before either of the console.logs in the done function.
console.log(JSON.stringify(theResult));
return theResult;
};
我想你在声明变量之前忘记了 "var"
var theResult = {}
您似乎正在对某些数据执行异步请求。任何时候执行异步 JavaScript,您都需要记住事情不是按顺序调用的。进行异步调用时,JavaScript 将继续执行堆栈下的代码。
在您的情况下,theResult
将是一个空对象,因为在您调用 console.log(JSON.stringify(theResult));
时 database.findOne(crit)
尚未完成执行
因此,您不能从 $scope.finder
return,而是可以将回调传递给 $scope.finder
并在 database.findOne(crit)
完成执行后执行。
$scope.finder = function (obj, callback) {
var id = obj.oid;
var crit = MC.Criteria('_ownerUserOid == ?', [id]);
theResult = {}; // Scope should be finder function.
database.findOne(crit) // This is a Monaca method for finding from database
.done(function(result) {
// Returns and empty object as expected.
console.log(JSON.stringify(theResult));
theResult = result;
callback(theResult);
// Returns the search result object as again expected.
console.log(JSON.stringify(theResult));
});
};
那就这样称呼吧
$scope.finder({some: 'data'}, function(response) {
// response now has the values of theResult
console.log(response)
});
改成这样:
$scope.finder = function (obj) {
return database.findOne(MC.Criteria('_ownerUserOid == ?', [obj.oid]));
};
// Client code:
finder({ oid: 'foo' })
.then(function(result) { console.log(JSON.stringify(result)); });
我对 javascript 范围/执行顺序有疑问。我在一个函数中创建了一个空对象。然后我在子函数中添加一些属性。但是,父函数中的属性没有改变。
$scope.finder = function (obj) {
var id = obj.oid;
var crit = MC.Criteria('_ownerUserOid == ?', [id]);
theResult = {}; // Scope should be finder function.
database.findOne(crit) // This is a Monaca method for finding from database
.done(function(result) {
// Returns and empty object as expected.
console.log(JSON.stringify(theResult));
theResult = result;
// Returns the search result object as again expected.
console.log(JSON.stringify(theResult));
});
// Here's the issue - when I log and return theResult, I get an empty object again.
// I think it has to do something with the scope.
// Also, this logs to console before either of the console.logs in the done function.
console.log(JSON.stringify(theResult));
return theResult;
};
我想你在声明变量之前忘记了 "var"
var theResult = {}
您似乎正在对某些数据执行异步请求。任何时候执行异步 JavaScript,您都需要记住事情不是按顺序调用的。进行异步调用时,JavaScript 将继续执行堆栈下的代码。
在您的情况下,theResult
将是一个空对象,因为在您调用 console.log(JSON.stringify(theResult));
database.findOne(crit)
尚未完成执行
因此,您不能从 $scope.finder
return,而是可以将回调传递给 $scope.finder
并在 database.findOne(crit)
完成执行后执行。
$scope.finder = function (obj, callback) {
var id = obj.oid;
var crit = MC.Criteria('_ownerUserOid == ?', [id]);
theResult = {}; // Scope should be finder function.
database.findOne(crit) // This is a Monaca method for finding from database
.done(function(result) {
// Returns and empty object as expected.
console.log(JSON.stringify(theResult));
theResult = result;
callback(theResult);
// Returns the search result object as again expected.
console.log(JSON.stringify(theResult));
});
};
那就这样称呼吧
$scope.finder({some: 'data'}, function(response) {
// response now has the values of theResult
console.log(response)
});
改成这样:
$scope.finder = function (obj) {
return database.findOne(MC.Criteria('_ownerUserOid == ?', [obj.oid]));
};
// Client code:
finder({ oid: 'foo' })
.then(function(result) { console.log(JSON.stringify(result)); });