typeof和instanceof的区别使用
typeof [] 和 type {}
[] instanceof Array
把数组变成键值的形式
let a = ['小明','小红']
console.log(JSON.stringify(Object.entries(a),null,2));
自己实现简单深拷贝
let user = {
name:'小明',
title:{
name:'标题'
},
arr:[1,2,3]
}
function copy(obj){
let res = obj instanceof Array ? [] : {}
for (const [k,v] of Object.entries(obj)) {
res[k] = typeof v === "object"?copy(v):v
console.log(res);
}
return res
}
let user2 = copy(user)
使用构造函数的抽象,保护参数和值不被随意改变
function user(name,age){
let data = {name}
this.age = age
let info = function (){
return data.age > 10 ? '傻逼':'大傻逼'
}
this.show = function (){
console.log(data.name + info());
}
}
let aa = new user('小明',20)
aa.name = '小红'
aa.age = 5
aa.info = function (){
return '我是大傻逼'
}
aa.show()
Object.entries()的使用
let arr = {name:'dashabi',age:101}
for (const [key,value] of Object.entries(arr)) {
console.log(key,value);
}
保护对象不可以添加新的属性
let obj = {name:'大傻逼'}
Object.preventExtensions(obj)
console.log(Object.isExtensible(obj));
保护对象不可以添加,删除属性
let obj = {name:'大傻逼'}
Object.seal(obj)
obj.age = 18
obj.name = '小煞笔'
delete name
console.log(obj);
console.log(Object.isSealed(obj));
对象全封闭(不可以删除,修改,添加)
Object.freeze(obj)
obj.name = '小煞笔'
console.log(obj);
console.log(Object.isFrozen(obj));
访问器得使用
const user = {
data:{name:'大傻逼',age:18},
set age(value){
value>=100?console.log('不行'):this.data.age = value
},
get age(){
return this.data.age
}
}
user.age = 102
console.log(user);
console.log(user.age);
console.log(user.age);
访问器的级别高
let user = {
data:{name:'大傻逼',age:18},
set name(value){
this.data.name = value + '小煞笔'
}
}
user.name = '三傻逼'
console.log(user);
结合symbol属性使用访问器
let DATA = Symbol()
let user = {
[DATA]:{name:'大傻逼',age:18},
set name(value){
this[DATA].name = value + '小煞笔'
}
}
console.log(user);
console.log(user[Symbol()].name);
构造函数中使用访问器
function User(name,age){
let data = {name,age}
Object.defineProperties(this,{
name:{
get(){
return data.name
},
set(value){
data.name = value
}
}
})
}
let aa = new User('小明',18)
aa.name = '小红'
console.log(aa.name);
对象代理
let aa = {name:'小明'}
let prory = new Proxy(aa,{
get(obj,property){
return obj[property] + '我是大傻逼'
},
set(obj,property,value){
obj[property] = value+'大傻逼吗你'
}
})
console.log(prory.name);
prory.name = '小红'
函数代理
let aa = {name:'小明'}
let prory = new Proxy(aa,{
get(obj,property){
return obj[property] + '我是大傻逼'
},
set(obj,property,value){
obj[property] = value+'大傻逼吗你'
}
})
console.log(prory.name);
prory.name = '小红'
console.log(prory.name);
代理对函数进行控制
function bb(num){
console.log('进来');
console.log(num);
return num
}
let prory1 = new Proxy(bb,{
apply(func,obj,args){
console.log(func);
console.log(obj);
console.log(args);
func.apply(obj,args)
}
})
prory1.apply({},[100])
对数组继续代理
let arr = [
{name:'小明',age:18},
{name:'小绿',age:20},
{name:'小宝',age:25}
]
let prory = new Proxy(arr,{
get(arr,key){
return arr[key].name
}
})
console.log(prory[1]);
使用代理实现双向绑定
<body>
<input type="text" v-model= "title">
<input type="text" v-model= "title">
<h1 v-bind="title"></h1>
</body>
<script>
function aa(){
let prory = new Proxy({},{
get(obj,properry){
console.log(obj);
},
set(obj,properry,value){
document.querySelectorAll(`[v-model="${properry}"]`).forEach(item=>{item.value = value})
document.querySelectorAll(`[v-bind="${properry}"]`).forEach(item=>{item.innerHTML = value})
},
})
this.init = function(){
let eles = document.querySelectorAll('[v-model]')
eles.forEach(item=>{
console.log(item);
item.addEventListener('keyup',function(){
prory[this.getAttribute('v-model')] = this.value
})
})
}
}
new aa().init()
</script>
使用代理工厂来做input验证规则
<style>
.aa{
border: solid 10px red;
}
</style>
<script>
class Validate{
max(value,len){
return value.length<len
}
min(value,len){
return value.length>len
}
isNumber(value){
return /^\d+$/.test(value)
}
}
function ProxyFa(target){
return new Proxy(target,{
get(target,key){
return target[key]
},
set(target,key,el){
let rule = el.getAttribute('rule')
console.log(rule);
let validate = new Validate()
let state = rule.split(',').every(rule=>{
let info = rule.split(':')
return validate[info[0]](el.value,info[1])
})
console.log(state);
el.classList[state?'remove':'add']('aa')
}
})
}
let proxy = ProxyFa( document.querySelectorAll('[validate]'))
proxy.forEach((item,i)=>{
item.addEventListener('keyup',function(){
proxy[i] = this
})
})
</script>
JSON格式的使用,作为各开发语言沟通的桥梁
JSON.stringify
let aa = {
name:'xiaming',
age:10
}
console.log(JSON.stringify(aa,['name'],2));
let bb = ['小明','大傻逼']
console.log(JSON.stringify(bb,null,2));
自定义返回toJSON的使用
let cc = {
name:'xiaming',
age:10,
tea:{
name1:'小红'
},
toJSON:function(){
return {
name:this.tea.name1,
age:this.age
}
}
}
console.log(JSON.stringify(cc,null,2));
JSON.parse
let dd = {
name:'xiaming',
age:10,
tea:{
name1:'小红'
}
}
let json = JSON.stringify(dd,null,2)
let obj = JSON.parse(json,(key,value)=>{
if (key === 'name') {
value = '小明'+value
}
return value
})
console.log(obj);