//the original function, operation success
Watermark (['/img/forest. JPG ', '/img/logo. PNG])
Image (watermark. Image. LowerRight (0.5))
Then (function (img) {
Var pre=document. QuerySelector (' # alpha - image pre ');
The pre. ParentNode. InsertBefore (img, pre);
});
//to modify the following function, read function, change the variable outside;
//the problem is: in the watermark can not access the variable, nor alert (), but can the console. The log ().
Var fistvar=11;
Var secondvar=22;
Watermark (['/img/forest. JPG ', '/img/logo. PNG])
Image (watermark. Image. LowerRight (0.5))
Firstvar=33;//problem a: absolutely wrong here?
Then (function (img) {
Secondvar=44;//the problem 2: secondvar variable defined here can't give out the assignment?
Var pre=document. QuerySelector (' # alpha - image pre ');
The pre. ParentNode. InsertBefore (img, pre);
Alert (secondvar. Tostring ());//question 3: this function can't alert
});
Alert (fistvar. Tostring ());//value or 11
Alert (secondvar. Tostring ());//value or 22
</script>
CodePudding user response:
This is the method of object chain called watermark (), image (). Then ();He just in order to facilitate reading,
Watermark ()
The image ()
Then ()
Among branches, writing is not a separate statement, can't insert firstvar=33; Other statements, such as
Then () function (img) code is the real function body within {},
Also, then () function should be asynchronous invocation,
Asynchronous invocation function should do not immediately, but let it at the back of the code execution,
Waiting for browser some work (such as: data download) processing is completed, then the function in the () method to execute,
For your code that is to say, then () method at the back of the
Alert (secondvar. ToString ());
Will be executed, the pop-up secondvar originally the value of the 22
After execution then () method of function secondvar=44