大家看一些这段代码:
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是把变量结果和方法执行的结果给打印出来。(执行期肯定先于结果期)
举个简单例子: