A detailed profile of an object
What is an object
A collection of pairs of variable names and values
var obj = {
name: 'Fitz'.age: 20
}
Copy the code
Why use objects
Better management of associated groups of data
// Use objects to manage student Fitz's personal information
var Fitz = {
name: 'Fitz'.age: 21.major: 'E-commerce'
}
// If you declare directly in the global scope instead of using objects, it will be difficult to make connections between data
var name = 'Lx'
var age = 21
var major = 'International commerce'
Copy the code
Object composition
- attribute
- methods
A method is a special kind of property, special in that its property value is a function
// Object composition
var obj = {
name: 'Fitz'.// Attributes in the obj object
wantToFindJob: function () { // methods in obj objects
console.log('Fitz wants to find front-end work ')}}Copy the code
How properties and methods in an object are accessed
Access, this method is simple to code, can be concise, fast access to the attributes of the object
var person = {
name: 'Fitz',
sayHello () {
console.log('hello')}}console.log(person.name) //'Fitz'
person.sayHello() // 'hello'
Copy the code
Note: If the attribute name is a variable or a special variable name such as Content-Type, the attribute value must be obtained by []
var person = {
name: 'Fitz'.age: 21
}
console.log(person['age']) / / 21
for (let i in obj){
console.log(person[i]) // 'Fitz' 21
}
Copy the code