作者red0whale (red whale)
看板Ajax
標題[問題] 提升傳統javascript執行效率的方法
時間Fri Feb 17 16:32:02 2017
像jquery,原先如果是這樣:
$("#div").css({"background-color":"#00FF00","color":"#FF0000"});
$("#div").animate({"width":"200px","height":"100px"},1000);
$("#div").on("click",function(){
console.log("clicked");
});
要簡化並提升它的執行效率的話就可以使用「方法鏈串」:(可以省去不必要的重複分析
目標元素)
$("#div").css({"background-color":"#00FF00","color":"#FF0000"}).animate({"widt
h":"200px","height":"100px"},1000).on("click",function(){
console.log("clicked");
});
那如果是傳統的javascript呢?
document.getElementById("div").style.backgroundColor = "#00FF00";
document.getElementById("div").style.color = "#FF0000";
....
是不是只要將「document.getElementById("div")」用一個變數取代就可以了
還是要用其他方法?
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 163.13.48.40
※ 文章網址: https://www.ptt.cc/bbs/Ajax/M.1487320326.A.FC0.html
→ MangoTW: 關鍵不在鏈式寫法而在你select幾次,變數避免重複select 02/17 20:26
推 Qiqi: 你想一下怎麼自己實做chain的設計,你就可以回答你的問題了 02/17 20:48
→ Qiqi: 但一樓的回應才是重點 02/17 20:49
→ async: 你已經自己解答了 02/18 15:31