Object (Object)
Objects are separated by curly braces, inside which the properties of the object are defined in the form of name: value, also known as JSON. Properties are separated by commas, and Spaces and folds are irrelevant. Declarations can span multiple lines.
Define an object name: Keafmd, age: 18, address: Beijing, isEdu:false
Sample code:
Var Ke = {'name': 'Keafmd', 'age': 18, address: 'Beijing ', isEdu:false} console.log(Ke)Copy the code
Complete code:
<! DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script> var Ke = { 'name': 'Keafmd', 'age': 18, address: 'Beijing ', isEdu:false} console.log(Ke) </script> </head> <body> </body> </ HTML >Copy the code
Effect screenshot:
Object creation
Create with {}
var person = {
name : 'Keafmd',
sayHi:function(){
console.log('hi, my name is :'+this.name)
}
};
Copy the code
Sample code:
<! DOCTYPE html> <html> <head> <meta charset="utf-8"> <script> var person = { name : 'Keafmd', sayHi:function(){ console.log('hi, my name is :'+this.name) } }; console.log(person) person.sayHi() </script> <title></title> </head> <body> </body> </html>Copy the code
Effect screenshot:
Create with Object
var p = new Object();
p.name = 'Keafmd';
p.age = 18;
console.log(p);
Copy the code
Sample code:
<! DOCTYPE html> <html> <head> <meta charset="utf-8"> <script> var p = new Object(); p.name = 'Keafmd'; p.age = 18; console.log(p); </script> <title></title> </head> <body> </body> </html>Copy the code
Effect screenshot:
Create using Function
function Student(){ this.name = ''; this.age = 0; } var stu1 = new Student(); stu1.name = "Keafmd"; stu1.age = 18; Stu1.address = 'Harbin '; stu1.address =' Harbin '; console.log(stu1); var stu2 = new Student(); console.log(stu2);Copy the code
Sample code:
<! DOCTYPE html> <html> <head> <meta charset="utf-8"> <script> function Student(){ this.name = ''; this.age = 0; } var stu1 = new Student(); stu1.name = "Keafmd"; stu1.age = 18; Stu1.address = 'Harbin '; stu1.address =' Harbin '; console.log(stu1); var stu2 = new Student(); console.log(stu2); </script> <title></title> </head> <body> </body> </html>Copy the code
Effect screenshot:
Use the class keyword
class Human{ constructor(name) { this.name = name; } sayHi(){console.log(' I am: '+this.name); } } var h1 = new Human('Keafmd'); h1.sayHi()Copy the code
Sample code:
<! DOCTYPE html> <html> <head> <meta charset="utf-8"> <script> class Human{ constructor(name) { this.name = name; } sayHi(){console.log(' I am: '+this.name); } } var h1 = new Human('Keafmd'); h1.sayHi() </script> <title></title> </head> <body> </body> </html>Copy the code
Effect screenshot:
The prototype model of the object
You can extend fields (properties, methods) to objects from objects
If you want to add attributes to the same type, use the prototype prototype
Sample code:
<! DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script> function Student(){ this.name = ''; this.age = 0; } var s1 = new Student(); SayHi = function(){console.log(' hello ')} s1.sayhi (); var s2 = new Student(); s2.sayHi(); </script> </head> <body> </body> </html>Copy the code
Effect screenshot:
Writing is not easy, read if it helps you, thank you for your support!
If you are a computer terminal, see the lower right corner of the “one button three links”, yes click it [ha ha]
Come on!
Work together!
Keafmd