-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.html
More file actions
72 lines (70 loc) · 2.01 KB
/
object.html
File metadata and controls
72 lines (70 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>object</title>
</head>
<body>
<script type="text/javascript">
// object 内置函数
var obj1 = {name:'张三'};
var obj2 = {age:18};
var obj3 = {say(){ console.log(1)}}
// 1. Object.assign(obj1,obj2,obj3)
// 可用于对象的拼接,将obj2,obj3......拼接到对象obj1上,并将obj1返回,obj1改变,其他对象不变。
var obj4 = Object.assign(obj1,obj2,obj3)
console.log(Object.assign(obj1,obj2,obj3))
console.log(obj4)
// 2. Object.is(a,b)
//用于判断两个值是否相等,与===类似 返回布尔值
Object.is(+0,-0) //false
Object.is(NaN,NaN) //false
// 3. object.defineProperty()
//直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象。
var obj = new Object()
Object.defineProperty(obj,'name',{
value:'李四'
})
// 4. object.defineProperties()
//直接在一个对象上定义一个或多个新的属性或修改现有属性,并返回该对象。
obj = new Object();
Object.defineProperties(obj, {
name: {
value: '张三',
configurable: false,
writable: true,
enumerable: true
},
age: {
value: 18,
configurable: true
}
})
console.log(obj.name, obj.age)
// 5. object.freeze(obj)
//组织修改现有属性的特性和值,并阻止添加新属性
obj={name:'张三',age:18};
Object.freeze(obj)
obj.name='李四';
obj.sex='男';
console.log(obj)
// 6. for in循环
obj = {
name:'ls',
age:12,
sex:'男'
}
for(var i in obj){
console.log(obj[i])
}
// 7. object.keys(obj)
//返回一个数组,包括对象自身的(不含继承的)所有可枚举属性(不含 Symbol 属性)的键名。
obj = {
name:'ls',
age:12,
sex:'男'
}
console.log(Object.keys(obj))
</script>
</body>
</html>