我对 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;
};