18 个高级工程师必须会的强大JavaScript 技巧
2023-08-14 22:01:28 软件 374观看
摘要浏览器01、实现全屏当您需要将当前屏幕显示为全屏时function fullScreen() { const el = document.documentElement const rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.moz

浏览器

HCo28资讯网——每日最新资讯28at.com

12、字符串脚本

当需要将一串字符串转换为JavaScript脚本时,该方法存在xss漏洞,请谨慎使用HCo28资讯网——每日最新资讯28at.com

const obj = eval('({ name: "jack" })')// obj will be converted to object{ name: "jack" }const v = eval('obj')// v will become the variable obj

13、递归函数名解耦

当需要编写递归函数时,声明了函数名,但是每次修改函数名时,总是忘记修改内部函数名。argument是函数的内部对象,包括传入函数的所有参数,arguments.callee代表函数名。HCo28资讯网——每日最新资讯28at.com

// This is a basic Fibonacci sequencefunction fibonacci (n) {    const fn = arguments.callee    if (n <= 1) return 1    return fn(n - 1) + fn(n - 2)}

DOM 元素HCo28资讯网——每日最新资讯28at.com

14、隐性判断

当需要判断某个dom元素当前是否出现在页面视图中时,可以尝试使用IntersectionObserver来判断。HCo28资讯网——每日最新资讯28at.com

<style>.item {    height: 350px;}</style><class="container">  <class="item" data-id="1">Invisible</div>  <class="item" data-id="2">Invisible</div>  <class="item" data-id="3">Invisible</div></div><script>  if (window?.IntersectionObserver) {    let items = [...document.getElementsByClassName("item")]; // parses as a true array, also available Array.prototype.slice.call()let io = new IntersectionObserver(      (entries) => {        entries.forEach((item) => {          item.target.innerHTML =            item.intersectionRatio === 1 // The display ratio of the element, when it is 1, it is completely visible, and when it is 0, it is completely invisible              ? `Element is fully visible`              : `Element is partially invisible`;        });      },      {        root: null,        rootMargin: "0px 0px",        threshold: 1, // The threshold is set to 1, and the callback function is triggered only when the ratio reaches 1      }    );    items.forEach((item) => io.observe(item));  }</script>

15、元素可编辑

当你需要编辑一个dom元素时,让它像文本区域一样点击。HCo28资讯网——每日最新资讯28at.com

<contenteditable="true">here can be edited</div>

16、元素属性监控

<id="test">test</div><button onclick="handleClick()">OK</button><script>  const el = document.getElementById("test");  let n = 1;  const observe = new MutationObserver((mutations) => {    console.log("attribute is changede", mutations);  })  observe.observe(el, {    attributes: true  });  function handleClick() {    el.setAttribute("style", "color: red");    el.setAttribute("data-name", n++);  }  setTimeout(() => {    observe.disconnect(); // stop watch  }, 5000);</script>

17、打印 dom 元素

当开发过程中需要打印dom元素时,使用console.log往往只能打印出整个dom元素,而无法查看dom元素的内部属性。你可以尝试使用 console.dirHCo28资讯网——每日最新资讯28at.com

console.dir(document.body)

其他HCo28资讯网——每日最新资讯28at.com

18、激活应用程序

当你在移动端开发时,你需要打开其他应用程序。还可以通过location.href赋值来操作以下方法HCo28资讯网——每日最新资讯28at.com

<a href="tel:12345678910">phone</a><a href="sms:12345678910,12345678911?body=hello">android message</a> <a href="sms:/open?addresses=12345678910,12345678911&body=hello">ios message</a><a href="wx://">ios message</a>

总结

以上就是我今天想与你分享的全部内容,希望对你有所帮助,最后,感谢你的阅读,祝编程愉快!HCo28资讯网——每日最新资讯28at.com

本文链接:http://www.28at.com/showinfo-26-5744-0.html18 个高级工程师必须会的强大JavaScript 技巧

声明:本网页内容旨在传播知识,不代表本站观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。

显示全文

上一篇:新兴技术趋势将彻底改变我们的世界

下一篇:SpringCloud Gateway 路由如何定位从底层源码分析

最新热点