简体   繁体   English

JavaScript函数范围/执行顺序问题

[英]Javascript function scope / order of execution issue

I'm having an issue with the javascript scope / order of execution. 我在javascript范围/执行顺序方面遇到问题。 I create an empty object inside a function. 我在函数内创建一个空对象。 Then I add some properties in a child function. 然后,在子函数中添加一些属性。 The properties haven't changed in the parent function, though. 但是,父函数中的属性未更改。

$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 = {} 

It looks like you are performing an asynchronous request for some data. 看来您正在执行一些数据的异步请求。 Any time you are performing asynchronous JavaScript, you will need to keep in mind that things aren't called sequentially. 每当您执行异步JavaScript时,都需要记住,事情不会被顺序调用。 When an asynchronous call is made, JavaScript will carry on executing code down the stack. 当进行异步调用时,JavaScript将在堆栈中继续执行代码。

In your case, theResult will be an empty object because database.findOne(crit) has not finished executing by the time you call console.log(JSON.stringify(theResult)); 在您的情况下, theResult将是一个空对象,因为在您调用console.log(JSON.stringify(theResult)); database.findOne(crit)尚未完成执行console.log(JSON.stringify(theResult));

Because of this, you cannot return from $scope.finder , instead you could pass a callback into $scope.finder and execute that once database.findOne(crit) has finished executing. 因此,您不能从$scope.finder返回,而是可以将回调传递给$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));
        });
};

Then call it like this. 然后这样称呼它。

$scope.finder({some: 'data'}, function(response) {
    // response now has the values of theResult
    console.log(response)
});

Change it to this: 更改为此:

$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)); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM