简体   繁体   中英

JavaScript calling a getter from its function

Trying to do something that in pseudo code would look like this:

(function(scope) {

    scope.doSomenthin = function() {

        if (x === y && this.onfinish) {
            //  If exists, run onfinish,  should return 'fin'
            this.onfinish();
        }
    }

})(scope);

window.scope = window.scope || (window.scope = {});
scope.doSomenthin().onfinish = function(){return 'fin'}

At run time if onfinish exists, run that function. Tried using getters/setter but at that point it will return undefined. Setting a timeout works but its not something I wish to do.

Any other ideas? Thanks.

I'm not sure if I completely understand the question, but I think what you want comes down to setting the context for the functions you are calling. Is this what you are after?

//create a function that accesses an object's properties and methods with 'this'
var doSomethin = function() {
    var result = "nonfinish";

    if (this.onfinish) {
        //  If exists, run onfinish,  should return 'fin'
      result = this.onfinish();
    }

    return result;
}

//add an 'onfinish' method to the 'scope' object
scope = {
    onfinish: function(){return 'fin'}
}

//run the accessor function in the window context
alert(doSomethin());

//run the accessor function in scope's context
alert(doSomethin.call(scope));

I see several mistakes with your code. This may be the results you are trying to achieve..

window.scope = window.scope || (window.scope = {});
scope.onfinish = function(){return 'fin'};

(function(scope) {

    scope.doSomenthin = function() {

        if (this.onfinish) {
            //  If exists, run onfinish,  should return 'fin'
            return this.onfinish();
        }
    }

})(scope);

alert(scope.doSomenthin());

  • When you create the temporary scope here you give scope as a parameter. But scope is not defined yet.

     (function(scope) { scope.doSomenthin = function() { if (x === y && this.onfinish) { // If exists, run onfinish, should return 'fin' this.onfinish(); } } })(scope); 
  • Your scope.doSomenthin function doesn't return any value. Because of that the value of scope.doSomenthin() is undifined. Therefore with scope.doSomenthin().onfinish = function(){return 'fin'} you are trying to set a property of undifined.

What you want to approach is similar to event-driven programming. Don't just call the function right away, register it as an event handler instead. The following pseudo-code only shows my idea. It's not complete

//register the function here, instead of calling it immediately
event = document.createEvent("HTMLEvents");
event.initEvent("myEvent", true, true);
document.addEventListener("myEvent", function(e) {
    e.scope.doSomenthin = function() {

        if (this.onfinish) {
            //  If exists, run onfinish,  should return 'fin'
            return this.onfinish();
        }
    }
});

......

//call the handler to handle the below event
window.scope = window.scope || (window.scope = {});
scope.doSomenthin().onfinish = function(){return 'fin'}
event.scope = scope;
document.body.dispatchEvent(event);

The above code is kind of silly. You have to design where to put and trigger the events.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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