JavaScript object
JavaScript Object
JavaScript object is data with properties and methods.
Objects, properties and methods in real life
In real life, a person is an object.
The object has its attributes, such as height and weight, etc., and its methods include work, rest, etc.
All people have these attributes, but they are different for everyone.
Everyone has these methods, but they are executed at different times.
JavaScript Objects
In JavaScript, almost everything is an object.
In JavaScript, objects are very important. When you understand objects, you can understand JavaScript.
You have learned about JavaScript variable assignment.
The following code sets the value of variable car to "Fiat":
var car = "Fiat";
The object is also a variable, but the object can contain multiple values ( multiple variables).
var car = {type:"Fiat", model:500, color:"white"};
In the above example, 3 values ("Fiat", 500, "white" ") is assigned to the variable car.
In the above example, 3 variables (type, model, color) are assigned to the variable car.
JavaScript objects are containers for variables.
Object definition
You can use characters to define and create JavaScript objects
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <p id="demo"></p> <script> var person = {firstName:"jack", lastName:"chen", age:35, height:"183cm"}; document.getElementById("demo").innerHTML = person.firstName + " 现在 " + person.age + " 岁."; </script> </body> </html>