First, the basic knowledge of the object
-
Concept: A particular thing, having characteristics (attributes) and behaviors (methods) that can store data.
-
Popular understanding: An object is a set of unordered key-value pairs that can store data.
-
Object's properties and methods: Can be . added in a way (this proves that JS is a dynamic type of language)
-
The value of most reference types in JS, all Object instances of type
Two, the object creation 3 ways 1.{}creating objects by literal volume
<script> var={}; obj2.name=‘zhangxin‘; console.log(obj2);</script>
2.newConstructors Create objects
- Constructor: First letter uppercase (Convention); Normal function: First letter lowercase
<script> var=newObject(); obj1.name=‘zhangxin‘; console.log(obj1);</script>
newConstructor, new do 4 things:
<Script> function Computer(Name,Age{ This.name =Name; This. Age =Age; This.say = function(){ return ' I am ' + This.name + '; this year ' + This. Age; } } varCp= New Computer(' computer ', -); Console.Log(CP.say()); //I am a computer; this year</script>
new 构造函数Creating an Array Object
<script> var arr = new Array(); console.log(arr); // [] </script>
3.Object.create({键值对})method to create a new object; ES5 method, IE8 and the following incompatible
<script> var o1 = {name: ‘zhangxin‘}; var create = Object.create(o1); console.log(create.name); // ‘zhangxin‘</script>
Third, the object's properties, methods (Assignment, take value) mode 1:.The way to add/read: Properties, method Mode 2:[]The way to add/read: Properties, methods(important)
-
Important: You can add dynamic property names, dynamic method names
-
Use [] the Add property, method: If the property name is fixed, quotation marks are required, otherwise it is considered a dynamic variable
<script> var obj = {}; obj.age = 20; obj[‘name‘] = ‘zhangxin‘; console.log(obj.age); // 20 console.log(obj[‘name‘]); // ‘zhangxin‘</script>
Four, the object loop traversalfor-in
<script> var={age:18,name:‘ss‘}; for (varin{ consloe.log(key); console.log(obj[key]); }</script>
V. Deep copy of ObjectObject.create( )
<script> var={name:‘zhangxin‘}; var=Object.create(o1); console.log=== o1); // false</script>
JS Reference type---Object