Javascript中实用的和新鲜的一些小知识 灰太狼 2022-06-05 22:37 130阅读 0赞 # # ### 1. 双位操作符 ### Math.floor(4.9) === 4 // true ======> ~~4.9 === 4 Math.cell(4.9) === 5 // true ======> ~4.9 === 5 ### **2. MutationObserver-------------------- > 在某个范围内的DOM树发生变化时作出适当反应** ### ** **new MutationObserver( function callback ); 详细了解([https://developer.mozilla.org/zh-CN/docs/Web/API/MutationObserver][https_developer.mozilla.org_zh-CN_docs_Web_API_MutationObserver]) ### **3. Object.freeze() ---------------> 冻结对象(不可改变其属性), 只冻结一层,若要冻结对象内的全部对象,可使用递归方法** ### ** **var obj = \{ a: ‘a’, b: ‘b’, func: function () \{ console.log(‘func’); \} \} var o = Object.freeze(obj); obj.a = ‘aa’; obj.a -------------> ‘a’ ### **4. every****方法/some方法** ### (接受两个参数,第一个是函数(接受三个参数:数组当前项的值、当前项在数组中的索引、数组对象本身),第二个参数是执行第一个函数参数的作用域对象,也就是上面说的函数中this所指向的值,如果不设置默认是undefined。) * every(): 该方法对数组中的每一项运行给定函数,如果该函数对每一项都返回 true,则返回true。 * some(): 该方法对数组中的每一项运行给定函数,如果该函数对任何一项返回 true,则返回true。 some方法是碰到一个返回true的值时候就返回了,并没有继续往下运行,而every也一样,第一个值就是一个false,所以后面也没有进行下去的必要了,就直接返回结果了。 例: let arr = \[ 1, 2, 3, 4, 5, 6 \]; console.log( arr.some( function( item, index, array )\{ console.log( 'item=' + item + ',index='+index+',array='+array ); return item > 3; \})); console.log( arr.every( function( item, index, array )\{ console.log( 'item=' + item + ',index='+index+',array='+array ); return item > 3; \})); [https_developer.mozilla.org_zh-CN_docs_Web_API_MutationObserver]: https://developer.mozilla.org/zh-CN/docs/Web/API/MutationObserver
还没有评论,来说两句吧...