JavaScript Objects1. DefinitionAn object is a reference data type and a container for storing complex data types. It is a collection of multiple attributes (data) and methods (functions). It allows dynamic addition and removal of attributes 2. Classification of objects
3. Define the objectObject literals
var obj={}; Constructor creates an object
var obj = new Object(); console.log(typeof obj); 4. Accessing properties in an objectPoint access symbol. o.name => 'terry' o.age => 12 [] Accessor o['name'] => 'terry' o['age'] => 12 If you read a property that does not exist in the object, no error will be reported but undefined will be returned. 5. Add properties to the objectobject.attribute = attribute value; o.gender = 'female'; 6. Deleting Object Propertiesdelete object.property; delete o.gender; 7.Object root constructorAll objects directly or indirectly inherit Object and can call methods in the Object prototype var o=new Object(); var arr = new Array(); 8. Detection of object attributes
'name' in Obj; 9. Reference passing and value passingBasic data types are passed by value: Basic data types store values directly in the stack area of memory The variables in js are all basic data types stored in the stack memory. The values are directly stored in the stack memory. The values exist independently. Modifying one variable will not affect other variables. var a=123; var b=a; a++; console.log('a='+a); //124 console.log('b='+b); //123 Reference data types are passed by reference: the reference address of the reference data type is stored in the stack area, and the actual value is stored in the heap area. The reference address points to the space in the heap area.
var obj = new Object(); obj.name='terry'; var obj2=obj; //Modify the name attribute of obj obj.name='tom'; console.log(obj.name); //tom console.log(obj2.name); //tom When comparing two primitive data types, the comparison value When comparing two reference data types, it compares the memory addresses of the objects. It will also return false if the two objects are identical but have different addresses. var c=10; var d=10; console.log(c==d); //true var obj3=new Object; var obj4=new Object; obj3.name='tom'; obj4.name='tom'; console.log(obj3==obj4); //false 10. Object serialization (converting objects into strings)
11. Enhanced for loop (for in)Iterating over objects
var obj = { name:'Sun Wukong', age:18, gender:'male', address:'Huaguoshan' } for(var n in obj){ console.log('Property name:'+n); console.log('Property value: '+obj[n]); } This is the end of this article about Script Objects in Java (details). For more relevant JavaScript object content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to run the springboot project in docker
The role of the a tag pseudo-class: ":link&qu...
Download the official website Choose the version ...
Before configuration, we need to do the following...
Float is often used in web page layout, but the f...
Table of contents Understand the core concept of ...
Table of contents 1. The concept of process and t...
123WORDPRESS.COM--HTML超文本标记语言速查手册<!-- --> !D...
Today I received a disk warning notification from...
Networks usage tutorial Official website docker-c...
Table of contents 1 Promise Interrupt the call ch...
This article example shares the specific code of ...
Table of contents think 1. Greedy Snake Effect Pi...
Test project: react-demo Clone your react-demo pr...
This article example shares the specific code for...
Table of contents 1. What is a calculated propert...