MutationObserver

time 0
面试的时候能多输出点就多输出点

通过词根词缀记单词
image.png

  1. <div id="app">
  2. <h1 id="title">Loading...</h1>
  3. </div>
  4. <script !src="">
  5. const oApp = document.getElementById('app');
  6. /*观察者对象,观察的内容之后会配置,把观察的内容告诉回调函数cb
  7. * 观察者观察到内容后,执行回调函数*/
  8. const observer = new MutationObserver(cb);
  9. function cb(mutationList, observer) {
  10. console.log(mutationList,observer)
  11. }
  12. // 观察节点的变化,得到观察的变化
  13. observer.observe(oApp, {
  14. attributes: true,
  15. childList: true,
  16. subtree: true
  17. })
  18. // 操作DOM
  19. const oTitle = oApp.querySelector('h1');
  20. oTitle.innerText = 'This is Title';//dom操作1 innerText
  21. oTitle.className = 'title';//dom操作2 修改className
  22. const oPara = document.createElement('p');//这个操作没有改变dom,检测不到,虽然是dom操作
  23. oPara.innerText = 'This is CONTENT';
  24. oApp.appendChild(oPara);//dom操作3 添加新的节点
  25. </script>

image.png

time 15m37s

  1. <div id="app">
  2. <h1 id="title">Loading...</h1>
  3. </div>
  4. <script !src="">
  5. const oApp = document.getElementById('app');
  6. /*观察者对象,观察的内容之后会配置,把观察的内容告诉回调函数cb
  7. * 观察者观察到内容后,执行回调函数*/
  8. const observer = new MutationObserver(cb);
  9. function callback(target) {
  10. console.log(target);
  11. }
  12. function cb(mutationList, observer) {
  13. // console.log(mutationList,observer)
  14. mutationList.forEach(mutation => {
  15. callback(mutation.target);
  16. })
  17. }
  18. // 观察节点的变化,得到观察的变化
  19. observer.observe(oApp, {
  20. attributes: true,
  21. childList: true,
  22. subtree: true
  23. })
  24. // 操作DOM
  25. const oTitle = oApp.querySelector('h1');
  26. oTitle.innerText = 'This is Title';//dom操作1 innerText
  27. oTitle.className = 'title';//dom操作2 修改className
  28. const oPara = document.createElement('p');//这个操作没有改变dom,检测不到,虽然是dom操作
  29. oPara.innerText = 'This is CONTENT';
  30. oApp.appendChild(oPara);//dom操作3 添加新的节点
  31. </script>

image.png

nextTick

time 18m33s
nodejs中运行

  1. process.nextTick(() => {
  2. console.log('nextTick1');
  3. })
  4. Promise.resolve().then(() => {
  5. console.log('promise');
  6. })
  7. process.nextTick(() => {
  8. console.log('nextTick2');
  9. })
  10. setTimeout(() => {
  11. console.log('setTimeout');
  12. }, 0)
  13. process.nextTick(() => {
  14. console.log('nextTick3');
  15. })

image.png

nextTick作为微任务是优先于promise运行的