javascript - Js执行顺序
本文介绍了javascript - Js执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
大家看一些这段代码:
function Parent3 () {
this.name = 'parent3';
this.play = [1, 2, 3];
}
Parent3.prototype.say = function(){console.log("aaa")};
function Child3 () {
Parent3.call(this);
this.type = 'child3';
}
Child3.prototype = new Parent3();
var s3 = new Child3();
var s4 = new Child3();
s3.play.push(4);
console.log(s3.play, s4.play,s3.say()); //预期顺序
就是段实现继承的代码,但问题不在这个,下面是代码的执行结果:
为什么先被打印出来的是aaa?
解决方案
看起来好像也没毛病
aaa是say执行过程中输出的,
而最后一句console是把变量结果和方法执行的结果给打印出来。(执行期肯定先于结果期)
举个简单例子:
这篇关于javascript - Js执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文