作为一名 JavaScript 开发人员,您可能意识到我们无法在不处理对象的情况下构建任何大型应用程序。
在 JavaScript 中,一切都是对象。
让我们深入了解 JavaScript 对象。
对象:JavaScript 中的对象只不过是一种非原始类型的数据结构。 我们可以使用花括号定义一个对象,并在其中放置用逗号分隔的键值对。
例如:
const user = { name:”Hemendra”, nickname:”Hemu”, email:”hi@aamchora.space”, city:”bhilwara”}
对对象的 CRUD 操作
创造
let student = {}; // an empty arraystudent.name = “Hemendra Khatik”; //important -> student[“name”]=”Hemendra Khatik” is also validstudent.branch = “CSE”;student.age = 25;console.log(student); // will print the below structure/*{ name:”Hemendra Khatik”, branch:”CSE”, age:25}*/
或一次所有键值
let user = { name:”Hemendra”, nickname:”Hemu”, email:”hi@aamchora.space”, city:”bhilwara”}
读
我们用 。 运算符来访问对象的值。
user.name; // to access the “Hemendra”.user.city; // to access the “bhilwara”.user.username; // will be undefined because it is not present in the user object.
user[“name”]; // to access the “Hemendra”.user[“city”]; // to access the “bhilwara”.
更新
更新一个对象。
student.age = 21; // now age is changed from 25 to 21
删除
从对象中删除键。
delete student.name; // to delete the name key from student object
其他有用的方法
仅从对象打印键。
const user = { username:”aamchora”, email:”hi@aamchora.space”,};Object.keys(user);/* Above expression returns an array containing keys only from an object [“username”, “email”]*/
仅从对象打印值。
const user = { username:”aamchora”, email:”hi@aamchora.space”,};Object.values(user);/* Above expression returns an array containing values only from an object [“aamchora”, “hi@aamchora.space”]*/
克隆对象
您不能像复制原始类型数据结构一样复制非原始类型数据结构。
例如:非原始类型数据结构。
let a = 10;let b = a;b= b + 1;console.log(a) // 10console.log(b) // 11
例如:原始类型数据结构。
let obj = { a:10;};let obj2 = obj;obj2.a = 12;if(obj === obj2){ console.log(“obj and obj2 are same”);};// above condition will be true console.log(obj) // {a:12}console.log(obj2) // {a:12}
我们可以使用 Object.assign(targetObject, sourceObject) 来克隆一个对象。
const obj = { a: 1 };const obj2 = Object.assign({}, obj);obj2.a = 2;if(obj === obj2){ console.log(“obj and obj2 are same”);}else{ console.log(“obj and obj2 are different”);};// above condition will be false here console.log(obj); // { a: 1 }console.log(obj2); // { a: 2 }
浅拷贝
对象的浅拷贝是一种拷贝,其属性与创建拷贝的源对象共享相同的引用(指向相同的底层值)。
注意:对于浅拷贝,我们使用 Object.assign(targetObject, sourceObject)。
看下面的例子来理解浅拷贝。
const obj = { a:1}const obj2 = { b:2, c:obj // here c’s value is a reference to the other object called obj}const copy = Object.assign({},obj2); // here obj2 is a source object // changing the copied object’s value copy.c.a = 1111;if(copy.c.a === obj.a){ console.log(“obj and obj2 are same”);}// above condition will be true
深拷贝
对象的深层副本是一个副本,其属性不共享与创建副本的源对象相同的引用(指向相同的基础值)。
注意:对于深拷贝,我们使用 JSON.parse(JSON.stringify(sourceObject))。
看下面的例子来理解深拷贝。
const obj = { a:1}const obj2 = { b:2, c:obj // here c’s value is a reference to the other object called obj}const copy = JSON.parse(JSON.stringify(obj2)) // here obj2 is a source object // changing the copied object’s value copy.c.a = 1111;if(copy.c.a === obj.a){ console.log(“obj and obj2 are same”);}else{ console.log(“obj and obj2 are not same”);}// above condition will be false
关注我以获取更多此类博客文章。
关注七爪网,获取更多APP/小程序/网站源码资源!