question: how to understand the delay execution of setTimeout
description: I want to see the specific use of the new method by changing the construction properties of the function prototype, but after triggering changAs ()
, I found that the constructor triggered by console.log (new As ());
before the changAs ()
statement has been changed
answer:
1. setTimeout
2. chrome,console.log(),i/o,,,chromeconsole.log(),,
then I intend to use setTimeout trigger delay execution to generate async, but it doesn"t work
eager answer:
- Why is setTimeout invalid here
- hopes to explain why it appears and triggers execution first, but the result is still changed
Thank you very much
Code:
function As() {
c = function(){
console.log("no.1");
};
a = 1 ;
a = 2;
return this;
};
As.prototype.As = "no.1";
As.prototype.c = function(){
console.log("no.1");
};
console.log(As);
console.log(typeof(As));
var s1 = As;
console.log(s1); //
console.log(As()); //window,returnundefined,
console.log(As.prototype);
As().c(); //no.1 //,underined.c()
console.log(As().a); // ,a,,window
console.log(new As()); //
new As().c(); //no.1
new new As().c(); //no.1
console.log(new As().As); //no.1
function changeAs(){
console.log("");
As.prototype.constructor = function(){
this.d = function(){
console.log("no.1");
}
return this;
}}
setTimeout(changeAs(), 2000);
console.log(new As());