大家好,欢迎来到IT知识分享网。
一、weakMap、map
Map、WeakMap:
Map是一种有序的键值对的集合,其中的键和值可以是任意类型的数据。键是唯一的,值可以重复。
Map通过set方法添加键值对,通过get方法获取值。使用size属性获取键值对的数量,clear方法用于清空所有键值对。如果值在集合中存在,返回true,否则false。delete方法判断val是否存在于集合中,如果存在就直接从集合中删掉,返回true
区别:
1、weakMap的key只能是引用类型,而map可以是任意类型(包括null,undefined)
2、weakMap不会被计入垃圾回收策略的,而map会计入,随着obj和aap的释放,weakMap也会随着消失。(但是有个问题你会发现控制台能输出,值是取不到的,应为V8的GC回收是需要一定时间的(最少200ms),你可以延长到500ms看一看,并且为了避免这个问题不允许读取键值,也不允许遍历,同理weakSet 也一样)
3、map是可迭代的,可以使用forEach和for…of,而weakMap是不可迭代的,不能使用forEach,for…of遍历
let obj: any = {
name: '孙亚龙' } //obj使用1次 let aap = obj //obj使用2次 let weakMap: WeakMap<object, any> = new WeakMap() weakMap.set(obj, 'saad') //obj还是算使用2次 如果使用的是map,则这里是计数为3 console.log(weakMap); obj = null //赋值为null表示引用次数-1
总结:
Map适合在需要存储键值对,并且需要获取键值对数量、清空键值对等特殊操作的场景。
WeakMap适合在需要存储键值对,并且需要键引用被销毁时自动删除键值对的场景。
二、weakSet、set
weakSet只有 add、delete、has方法,不能遍历等等
//1、set let set: Set<number> = new Set([1, 2, 2, 3, 4, 6]) //天然去重,引用类型除外(引用类型,地址相同也可以去重) console.log(set);
区别:
weakSet存引用类型,set可以存任何
垃圾回收机制同wekMap
三、proxy和Reflect
1.proxy
let person = {
name: 'aaa', age: 24 } // proxy支持对象、数组、函数、set、map person.name = 'xx' let personProxy = new Proxy(person, {
// 取值 get() {
}, // person name aaa person set(target, key, value, receiver) {
return true }, // 拦截函数的调用 apply() {
}, //拦截in操作符 // var a=1 // 'a' in window has() {
}, // 拦截for...in ownKeys() {
}, // 拦截new操作符 construct() {
}, // 拦截删除的操作 deleteProperty(target, p) {
} })
2.Reflect
1.Reflect 反射13个方法,参数一摸一样
let person = {
name: 'aaa', age: 24 } console.log(Reflect.set(person, "name", '小曼', person)); //返回boolean console.log(person.name, Reflect.get(person, "name", person)); // console.log(person.name); let personProxy = new Proxy(person, {
get(target, key, receiver) {
if (target.age <= 18) {
return Reflect.get(target, key, receiver) } else {
return '你大于18' } } }) console.log(personProxy.age); //你大于18
四、类型守卫
1.类型收缩|类型收窄
const isString = (str: any) => {
return typeof (str) === 'string' // return str instanceof Array }
2.类型谓词|自定义守卫
const isObject = (arg: any) => ({
}).toString.call(arg) === '[object Object]' //Object.prototype=({}) const isNumber = (num: any): num is number => typeof num === 'number' //Object.prototype=({}) const isString = (str: any): str is string => typeof str === 'string' //Object.prototype=({}) const isFn = (fn: any) => ({
}).toString.call(fn) === '[functon Object]' //Object.prototype=({})
const isObject = (arg: any) => ({
}).toString.call(arg) === '[object Object]' //Object.prototype=({}) const isNumber = (num: any): num is number => typeof num === 'number' //Object.prototype=({}) const isString = (str: any): str is string => typeof str === 'string' //Object.prototype=({}) const isFn = (fn: any) => ({
}).toString.call(fn) === '[functon Object]' //Object.prototype=({}) const fns = (data: any) => {
if (isObject(data)) {
let val; //遍历属性,不能用for..in,因为他会遍历原型上的属性 Object.keys(data).forEach(key => {
val = data[key] if (isNumber(val)) {
data[key] = val.toFixed(2) } if (isString(val)) {
data[key] = val.trim() } if (isFn(val)) {
data[key]() } }) //数组里放key } } let obj = {
a: 100., b: ' test ', c: function () {
console.log(this); return this.a } } fns(obj) console.log(obj);
最后:如果有问题或不理解的地方请大家指出,谢谢大家~
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/110252.html
