4 回答
TA贡献1862条经验 获得超6个赞
并不是你想的那样立即执行的,这种链式调用是前一个执行完毕,再执行后面的内容比如
var obj={
a:0,
parentNode:function(){
console.log(this.a);
return this;
},
appendChild:function(){
console.log(this.a);
return this;
},
removeChild:function(){
console.log(this.a);
return this;
}
}
obj.appendChild().parentNode().removeChild();
运行时都是等待上一部执行完成,再进入下一步
TA贡献2036条经验 获得超8个赞
TA贡献1850条经验 获得超11个赞
运行下代码,就知道是什么样的了
var a = 1,
code = 'a = 2;var d=100;while(d!=0) { d--;console.log("executing script");console.log(document.getElementById("myScript"));}; console.log("script done");',
doc = document,
body = doc.body,
script = doc.createElement('script');
script.setAttribute("id","myScript");
script.innerHTML = code;
script=body.appendChild(script);
console.log("append script");
console.log(document.getElementById("myScript"));
script.parentNode.removeChild(script);
console.log("remove script");
console.log(document.getElementById("myScript"));
console.log(a);
添加回答
举报