How does js define an object:

Use the var keyword JavaScript with a {… } represents an object, and the key-value pairs are declared in the form XXX: XXX, separated by,. Note that the last key-value pair does not need to be appended at the end, and some browsers (such as earlier versions of Internet Explorer) will report an error if it is appended.

varObject name ={property method <script>var xiaoming={
    name:"worker".age:23.salary:7000}; alert(worker.salary+worker.name+worker.age); < / script >};Copy the code
  • Conclusion:
  1. Remember the semicolon after the curly braces
  2. Each attribute is separated by a comma
  • Access to properties is through. Operator, but this requires that the attribute name be a valid variable name. If the attribute name contains special characters, it must be enclosed in ‘ ‘:
var xiaohong = { name: 'little red'.'middle-school': 'No.1 Middle School' 
 }; 
Copy the code

Xiaohong’s property name, middle-school, is not a valid variable, so it needs to be enclosed in “. Access to this property is also unavailable. Operator, which must be accessed with [‘ XXX ‘] :

xiaohong['middle-school']; // 'No.1 Middle School'
xiaohong['name']; // '/'
xiaohong.name; // '/'
Copy the code

Virtually all attributes of a JavaScript object are strings, but the values corresponding to the attributes can be of any data type. What is returned if a nonexistent property is accessed? If a property does not exist, it will return undefined:

var xiaoming = {
    name: 'Ming'
};
Copy the code

console.log(xiaoming.name);

console.log(xiaoming.age); // undefined

Since js is a dynamic type, you are free to add or remove attributes to objects:

<! DOCTYPE html><html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Document</title>
 <script>
var worker={
     name:"xiaoming".age:23.salary:7000.'middle-school': 'No.1 Middle School'
      };
     // Add attributes dynamically
     worker.time;
     worker.time=20;
     worker.weight=100;
     // Delete attributes
     delete worker.weight;
     // alert(worker.salary+worker.name+worker.age);
     document.write(worker.time+"<br />");
     document.write(worker.weight+"<br />");
     document.write(worker.name+"<br />");
     document.write(worker['name'] +"<br />");
     document.write(worker['middle-school'] +"<br />");
     document.write(worker.mother);
 </script>
</head>
<body>	
</body>
</html>
Copy the code

Results:

20

undefined

xiaoming

xiaoming

No.1 Middle School

undefined

  • How to tell if an attribute is inside an object: in

Format: ‘property’ in object name; If in determines that an attribute exists, it does not have to be inherent; it may be inherited:

  • If you want to determine whether there is a way

To determine if a property is owned by Xiaoming itself, rather than inherited, use the hasOwnProperty() method:

  • Worker. HasOwnOperty (” name “);

  • How do I traverse an object with in

<! DOCTYPE html><html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Document</title>
 <script>
    var worker={
    name:"xiaoming".age:23.salary:7000.'middle-school': 'No.1 Middle School'
    };
    // Add attributes dynamically
    worker.time;
    worker.time=20;
    worker.weight=100;
    // Delete attributes
    delete worker.weight;
    delete worker['weight'];
    // alert(worker.salary+worker.name+worker.age);
    document.write(worker.time+"<br />");
    document.write(worker.weight+"<br />");
    document.write(worker.name+"<br />");
    document.write(worker['name'] +"<br />");
    document.write(worker['middle-school'] +"<br />");
    document.write(worker.mother);
    // How to traverse objects
    for(var k in worker)
    {
    document.write(k+':'+worker[k]);
}
 </script>
</head>
<body>	
</body>
</html>
Copy the code

Results: 20

undefined

xiaoming

xiaoming

No.1 Middle School

undefinedname:xiaomingage:23salary:7000middle-school:No.1 Middle Schooltime:20