随笔分类 - js
摘要:1、for in遍历的是键。for in遍历数组得到的是下标,遍历对象得到的是key值 2、for of遍历的是值,和forEach一样直接得到值 3、for of只能遍历数组,不能遍历对象
阅读全文
摘要:1、使用闭包实现累加函数 function addFn() { let num = 0 return function() { console.log(num++) } } const add = addFn() add() // 0 add() // 1 add() // 2 或 function
阅读全文
摘要:捕获同步异常: created() { this.test1() }, methods: { test1() { try { this.test2() } catch (err) { console.warn('捕获到test2的错误', err) } }, test2() { throw new
阅读全文
摘要:一、原生js的方式: * { margin: 0; padding: 0; } body { height: 2000px; } .header { height: 100px; background-color: red; } .nav { line-height: 50px; backgroun
阅读全文
摘要:const a = [] for (i = 0; i < 100000; i++) { a.push({ id: i, name: 'xx' + i }) } const b = [] for (let i = 0; i < 100; i++) { b.push({ id: i, name: 'yy
阅读全文
摘要:html: <style> .div { width: 200px; height: 200px; background-color: greenyellow; } </style> </head> <body> <div class="div">一些文字</div> <script> const
阅读全文
摘要:export function shuffle(arr) { let _arr = arr.slice() for (let i = 0; i < _arr.length; i++) { const j = _getRandomNumber(0, i) const temp = _arr[i] _a
阅读全文
摘要:根据windows日历,获取当前周,以及当前周的前2周和后4周,共7周的日期范围 export function getFormatDate(serverDate) { let list = [] // 二维数组 let formatDate = function (date, days) { le
阅读全文
摘要:1、utils/utils.js const namespace = 'mall' export function setItem(key, value) { let storage = window.localStorage.getItem(namespace) storage = storage
阅读全文
摘要:export const quertObject = (url) => { const queryString = url.split('?')[1] const obj = {} const arr = queryString.split('&') for (let i = 0; i < arr.
阅读全文
摘要:<ul id="list"> <li id="li1">项目一</li> <li>项目二</li> <li>项目三</li> <li>项目四</li> </ul> const list = document.getElementById('list') const li1 = document.ge
阅读全文
摘要:<ul id="list"></ul> const ul = document.getElementById('list') const fragment = document.createDocumentFragment() for (let i = 0; i < 5; i++) { const
阅读全文
摘要:class Person { constructor(name) { this.name = name } publicFn() { console.log('公共方法') } } class Student extends Person { constructor(name, score) { /
阅读全文
摘要:hash: <button id="myBtn">按钮</button> <script> // 监听hash的变化:手动去改路由、浏览器前进后退、点击事件更改hash window.onhashchange = (e) => { console.log('老url', e.oldURL) cons
阅读全文
摘要:[].forEach.call($$("*"),function(a){ a.style.outline="1px solid #"+(~~(Math.random()*(1<<24))).toString(16) })
阅读全文
摘要:function star(rate){ const star='★★★★★☆☆☆☆☆' return star.slice(5-rate,10-rate) }
阅读全文
摘要:扩展运算符的剩余参数,如果想删除对象中的某个属性,这是一个思路 可以对原对象中的字段重新赋值,以及添加一个新的字段 const obj = { name: 'xx', age: 12 } const o = { ...obj, name: 'yy', hobby: 'ss' } // 重写name,
阅读全文
摘要:利用对象对数组的每一项进行解构,可以方便地获取数组的第n个值
阅读全文
摘要:const flatten = (arr, depth = 1) => depth != 1 ? arr.reduce((a, v) => a.concat(Array.isArray(v) ? flatten(v, depth - 1) : v), []) : arr.reduce((a, v)
阅读全文
摘要:var cars = ['BMW','Benz', 'Benz', 'Tesla', 'BMW', 'Toyota']; var carsObj = cars.reduce(function (obj, name) { obj[name] = obj[name] ? ++obj[name] : 1;
阅读全文