日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當(dāng)前位置:首頁 > 科技  > 軟件

18 個高級工程師必須會的強(qiáng)大JavaScript 技巧

來源: 責(zé)編: 時間:2023-08-14 22:01:28 394觀看
導(dǎo)讀瀏覽器01、實現(xiàn)全屏當(dāng)您需要將當(dāng)前屏幕顯示為全屏?xí)rfunction fullScreen() { const el = document.documentElement const rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.moz

瀏覽器

FGc28資訊網(wǎng)——每日最新資訊28at.com

12、字符串腳本

當(dāng)需要將一串字符串轉(zhuǎn)換為JavaScript腳本時,該方法存在xss漏洞,請謹(jǐn)慎使用FGc28資訊網(wǎng)——每日最新資訊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、遞歸函數(shù)名解耦

當(dāng)需要編寫遞歸函數(shù)時,聲明了函數(shù)名,但是每次修改函數(shù)名時,總是忘記修改內(nèi)部函數(shù)名。argument是函數(shù)的內(nèi)部對象,包括傳入函數(shù)的所有參數(shù),arguments.callee代表函數(shù)名。FGc28資訊網(wǎng)——每日最新資訊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 元素FGc28資訊網(wǎng)——每日最新資訊28at.com

14、隱性判斷

當(dāng)需要判斷某個dom元素當(dāng)前是否出現(xiàn)在頁面視圖中時,可以嘗試使用IntersectionObserver來判斷。FGc28資訊網(wǎng)——每日最新資訊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、元素可編輯

當(dāng)你需要編輯一個dom元素時,讓它像文本區(qū)域一樣點擊。FGc28資訊網(wǎng)——每日最新資訊28at.com

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

16、元素屬性監(jiān)控

<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 元素

當(dāng)開發(fā)過程中需要打印dom元素時,使用console.log往往只能打印出整個dom元素,而無法查看dom元素的內(nèi)部屬性。你可以嘗試使用 console.dirFGc28資訊網(wǎng)——每日最新資訊28at.com

console.dir(document.body)

其他FGc28資訊網(wǎng)——每日最新資訊28at.com

18、激活應(yīng)用程序

當(dāng)你在移動端開發(fā)時,你需要打開其他應(yīng)用程序。還可以通過location.href賦值來操作以下方法FGc28資訊網(wǎng)——每日最新資訊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>

總結(jié)

以上就是我今天想與你分享的全部內(nèi)容,希望對你有所幫助,最后,感謝你的閱讀,祝編程愉快!FGc28資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-5744-0.html18 個高級工程師必須會的強(qiáng)大JavaScript 技巧

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 新興技術(shù)趨勢將徹底改變我們的世界

下一篇: SpringCloud Gateway 路由如何定位從底層源碼分析

標(biāo)簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 锦州市| 扶余县| 建德市| 礼泉县| 共和县| 麟游县| 玉林市| 济阳县| 化德县| 唐河县| 冀州市| 阿尔山市| 靖州| 抚顺市| 兴国县| 恩平市| 吐鲁番市| 修武县| 湘潭县| 乡城县| 四会市| 钟山县| 宝丰县| 德江县| 永兴县| 黑河市| 化隆| 揭西县| 蕲春县| 黔南| 崇阳县| 探索| 大埔县| 永登县| 邵武市| 连山| 大田县| 赫章县| 尼玛县| 农安县| 濮阳县|