A girl called fairy and a helpless pain buried in the code of the monkey program dialogue:
Girl: Are you short of dates
Program ape: lack object is not simple yao, oneself create a good
Girl:…
Get to the code!
There are currently three ways to create objects in JavaScript
- Create objects using literals
- Create an Object using new Object
- Use constructors to create objects
Create objects using literals
Object literals: curly braces {} contain properties and methods that represent the object.
case
<script type="text/javascript"> var dog={name:" coco ", type:" alasky ", age:5, color:"red", skill1:function(){ console.log("bark") }, skill2:function(){ console.log("showFilm") } } alert(dog.name); alert(dog['age']); // Access the attribute dog.skill1(); // Run method </script>Copy the code
Create object
(1) The properties or methods inside are in the form of key-value pairs. Key: attribute name value: attribute value
(2) Multiple attributes or methods must be separated by commas
(3) The method colon is followed by an anonymous function
Using the object
(1) Call the properties of the object we take the object name. The property name is understood as console.log(the object name). The property name)
Object name [‘ property name ‘]
(3) The name of the method object calling the object. The method name