9.对象的扩展方法.html 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. // Object.is() 判断两个值是否全等
  11. const obj1 = { name: "图图", age: 3 };
  12. const obj2 = { name1: "刷子", age1: 4 };
  13. console.log(Object.is(obj1, obj2)); // false
  14. console.log(Object.is(1, "1")); // false
  15. // Object.assign() 对象合并 若后面传入的对象中与前面的对象有相同的属性 则覆盖 其余的合并
  16. console.log(Object.assign(obj1,obj2));
  17. // const x = {
  18. // name:[1,2,3]
  19. // };
  20. // console.log(obj2,'obj2')
  21. // Object.setPrototypeOf(obj2,x);
  22. // console.log(Object.getPrototypeOf(obj2));
  23. const obj3 = {
  24. name:"哆啦A梦",
  25. age:1000
  26. }
  27. console.log(obj3,'obj3')
  28. // 返回可迭代的属性的键值对数组
  29. console.log(Object.keys(obj3),'obj3')
  30. // 返回可迭代的值的键值对数组
  31. console.log(Object.values(obj3),'obj3')
  32. // 返回可迭代的对象的键值对数组
  33. console.log(Object.entries(obj3),'obj3')
  34. // 返回当前传入对象的自身属性
  35. console.log(Object.getOwnPropertyDescriptors(obj3),'obj3')
  36. // Object.create(当前对象的原型 一般为null,创建的对象) 创建一个新对象
  37. const obj4 = Object.create(null,{
  38. name:{
  39. value:"喜羊羊",
  40. enumerable:true,
  41. configurable:true,
  42. writable:true
  43. },
  44. friends:{
  45. value:["美羊羊","懒羊羊"],
  46. enumerable:true,
  47. configurable:true,
  48. writable:true
  49. }
  50. })
  51. console.log(obj4,'obj4')
  52. console.log(Object.getOwnPropertyDescriptors(obj4),'obj4')
  53. </script>
  54. </body>
  55. </html>