当前位置:网站首页>js 对象 使用小技巧
js 对象 使用小技巧
2022-06-23 15:27:00 【豆趣编程】
js中中,Object,Array ,Function 等都属于引用类型,他们的变量名都是指向对象的指针。
这样就有一个好处,当处理一个复杂json树的时候,想要单独改变其中某一个子对象属性时,不需要根据对象id遍历查找到这个对象了,而是可以直接通过事件方式将这个对象通过参数的方式赋值给一个专属变量,这个变量就指向这个对象,这样就可以随意改变对象属性了。改变这个变量对应的对象,整个json树中的这个对象也被相应的改变。
下面举个栗子,有点类似于双向绑定,点击哪个对象就可以单独修改这个对象,修改后会在json树中更新数据

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div >
<ul id="classmates">
</ul>
<div class="edit">
<span>姓名:</span>
<input type="text" value="" name="name"/>
<br />
<span>年龄:</span>
<input type="text" value="" name="age"/>
</div>
</div>
<script type="text/javascript">
//保存选中的同学
var classmate={
name:"",
age:""
}
//同学列表
var arr=[
{
name:"小明",
age:22
},
{
name:"小黑",
age:23
},
{
name:"小红",
age:24
},
{
name:"小白",
age:25
}
];
var nameNode=document.querySelector("input[name='name']");
var ageNode=document.querySelector("input[name='age']");
nameNode.addEventListener("keyup",function(){
classmate.name=nameNode.value;
update()
//console.log(classmate)
})
ageNode.addEventListener("keyup",function(){
classmate.age=ageNode.value;
update()
})
//进行双向绑定
Object.defineProperty(classmate,'name',{
get:function(){
return classmate['name']
},
set:function(val){
classmate['name'] = val;
}
})
Object.defineProperty(classmate,'age',{
get:function(){
return classmate['age']
},
set:function(val){
classmate['age'] = val;
}
})
//重绘ul列表
function update(){
document.querySelector("#classmates").innerHTML="";
for(var i=0;i<arr.length;i++){
var classmate=arr[i];
var li="<li>姓名:"+classmate.name + ";年龄:"+classmate.age +"</li>";
var liNode=parseElement(li);
liNode.addEventListener("click",showClassmate(classmate))
document.querySelector("#classmates").appendChild(liNode)
}
}
update()
//点击同学
function showClassmate(data){
return function(){
classmate=data;
nameNode.value=classmate.name;
ageNode.value=classmate.age;
}
}
//将字符串变成node对象
function parseElement(htmlString){
return new DOMParser().parseFromString(htmlString,'text/html').body.childNodes[0]
}
</script>
</body>
</html>
这里面的json数据较简单,如果面对复杂的多层关系树,通过这种方式修改数据会很方便
边栏推荐
- Web篇_01 了解web開發
- OpenResty 基础
- 创建好后的模型,对Con2d, ConvTranspose2d ,以及归一化BatchNorm2d函数中的变量进行初始化
- readImg: 读取图片到Variable变量
- Which platform is a good place to open a futures account? Is it safe to open an online futures account?
- npm 如何发包 删包
- Half wave loss equal thickness and equal inclination interference
- mysql 系列:总体架构概述
- Important knowledge of golang: rwmutex read / write lock analysis
- 139. word splitting
猜你喜欢

【无标题】激光焊接在医疗中的应用

Arrays in JS

stylegan2:analyzing and improving the image quality of stylegan
Explain in detail the principle and implementation of redis distributed lock
![[MAE]Masked Autoencoders掩膜自编码器](/img/08/5ab2b0d5b81c723919046699bb6f6d.png)
[MAE]Masked Autoencoders掩膜自编码器

Important knowledge of golang: waitgroup parsing

XML

golang 重要知识:atomic 原子操作

golang 重要知识:RWMutex 读写锁分析

Slice() and slice() of JS
随机推荐
Detailed steps for MySQL dual master configuration
Shandong: food "hidden money", consumption "sweeping monk"
图片读取:Image.open(ImgPath)
现在我要买股票,怎么开户?手机开户安全么?
System design and analysis - Technical Report - a solution for regularly clearing verification code
C. Product 1 Modulo N-Codeforces Round #716 (Div. 2)
Android kotlin collaboration Async
请问期货开户去哪个平台好?网上期货开户安全吗?
VIM backup history command
F5《2022年应用策略现状报告》:边缘部署及负载安全成亚太地区关注焦点
Important knowledge of golang: atomic atomic operation
子级文件拖到上一级
Sorting out and summarizing the handling schemes for the three major exceptions of redis cache
Which platform is a good place to open a futures account? Is it safe to open an online futures account?
Explore the "store" on the cloud. The cloud store is newly upgraded!
Understand the classic buck-boost negative voltage circuit
golang 重要知识:waitgroup 解析
证券开户的优惠怎样才能得到?在线开户安全么?
30. 串联所有单词的子串
139. 單詞拆分