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 = 0
window.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
}
})()