0

How is the then() function executed? Is it different if I call it with referring to this object?

I mean:

casper.then(function() {
        casper.then(function(){
            // stuff
            this.then(function() {
                // stuff2
            });
            casper.then();
        });
});

casper.then(function() {
        this.then(function(){
            casper.then();
            this.then();
            // stuff
        });
});

Is it the same (execution order?)? It should be, isn't it?

szab.kel
  • 2,356
  • 5
  • 40
  • 74

1 Answers1

0

There is no real difference. Most of the time it is just a shortcut (2 characters).

Every callback is bound to the calling casper object. this is essentially local scope, so the execution environment doesn't need to search for it in other higher scopes. Whereas the casper reference is defined in the global scope and requires a search which takes a little time. This is a good write-up of scopes.

There are rare occasions that you need to use the global casper variable instead of this. Here is an example for that.

Beware: If you are in the test environment this in the casper.test.begin callback will reference casper.test.

Community
  • 1
  • 1
Artjom B.
  • 61,146
  • 24
  • 125
  • 222