学习记录贴

摘录1

JSON.stringify 处理不安全的json对象,会报错,此时可以在JSON对象中添加一个函数,toJSON,在转换的时候,默认会先调用toJSON方法
位运算符 ~

42 相当于 -(42+1) , -(x+1)
实际应用为:if(~'hello'.indexOf('h')) { // 找到匹配}
~~x 和 x | 0 在进行取整的作用上基本一致,但是由于运算符优先级考虑,更多使用~~进行转换

this相关

// 箭头函数的函数绑定无法被修改
var a = 123
var foo = ()=>a=> console.log(this.a) // 123
var bar = foo.call({a:2})
console.log(bar.call({a:3})). // 123

// 使用const声明的对象不会挂载到window对象上
const a = 123
var foo = ()=>a=> console.log(this.a) //undefined
var bar = foo.call({a:2})
console.log(bar.call({a:3})) // undefined

垃圾回收机制

隐式转换

// “+” 号右边向左边类型转换 
1 + false  // 1 + Number(false)
1 + undefined // NaN
'1' + false // 1false

setTimeout

setTimeout最小延迟时间为4ms

// 相当于 eval
(function() {
  setTimeout('var something2 = 20', 1000);
})();
// something2 => 20

vue2 中 vuex 和 data 常遇到的bug

esm 是导出的引用,而Commonjs导出的是值的拷贝

import { file_config } from './config'
data(){
  return {
    array:[],
    file_config,
    tableData:[]
  }
},
methods:{
  request(){
    /*
     * 遇到这个问题是在两个tab页面都打开了,里面的table在第二个tab打开时,tableData数据变成一样了
   */
    const list = file_config;   // 应该使用深拷贝,避免污染 const list =cloneDeep(file_config); 
    list.forEach(v=>v.id = v.typeId)
    this.tableData = list;
  }
}

//---------- vuex   ----------
//state
const state = {
  isCanEditObj:{}
}

// action
const current = {} 
 // 引用类型和基本类型区别
 current[applyNo] = value
 const obj = Object.assign({}, state.isCanEditObj, current)
 state.isCanEditObj = obj

// 如果这里直接
state.isCanEditObj[applyNo] = value 
// 那么页面中watch state.isCanEditObj 不会发生任何变化

//---------- data   ----------
this.array.push(1) // 此处也一样,watch之类的监听不到变化,需要$set 或者直接 整体重新赋值this.arrary = ...

Vue 结构图

posted @ 2021-07-09 09:05  1146937621  阅读(19)  评论(0)    收藏  举报