js常见需求开发
1.动态js插入script标签
const script = document.createElement('script')
script.src = 'static/echarts.js'
script.onload = this.initEcharts.bind(this)
document.head.appendChild(script)
2.下载exal的实现(导出功能)
_downloadExcal (url, data) {if (url && data) {// if (NODE_ENV === 'production') {// url = url.replace('/api', '')// }let form = document.createElement('form')form.setAttribute('action', url)form.setAttribute('method', 'post')for (let item in data) {let val = data[item] === undefined ? '' : data[item] === null ? '' : data[item]let input = document.createElement('input')input.setAttribute('type', 'text')input.setAttribute('name', item)input.setAttribute('value', val)form.appendChildn(input) // 注意ie的问题}document.body.appendChild(form)form.submit()document.body.removeChild(form)}}
3.转数字字符串的快捷方法
num-0 转化为数字
num+'' 转化为字符串
4.熟知问题:
说明:只要bind绑定会返回一个新的函数,但是一旦绑定bind,就无法通过call或apply再次修改this,箭头函数也是不能修改this
5.requestAnimationFrame兼容写法:
if (!Date.now)Date.now = function() {return new Date().getTime()};(function() {'use strict'var vendors = ['webkit', 'moz']for (var i = 0;i < vendors.length && !window.requestAnimationFrame;++i) {var vp = vendors[i]window.requestAnimationFrame = window[vp + 'RequestAnimationFrame']window.cancelAnimationFrame =window[vp + 'CancelAnimationFrame'] ||window[vp + 'CancelRequestAnimationFrame']}if (/iP(ad|hone|od).*OS 6/.test(window.navigator.userAgent) || // iOS6 is buggy!window.requestAnimationFrame ||!window.cancelAnimationFrame) {var lastTime = 0window.requestAnimationFrame = function(callback) {var now = Date.now()var nextTime = Math.max(lastTime + 16, now)return setTimeout(function() {callback((lastTime = nextTime))},nextTime - now)}window.cancelAnimationFrame = clearTimeout}})()
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
