Create a new instance person and add four attributes to it:
1 person=new Object (); 2 person.firstname= "Bill"; 3 person.lastname= "Gates"; 4 person.age=56; 5 person.eyecolor= "Blue";
Person{firstname: "John", LastName: "Doe", Age:50,eyecolor: "Blue"};
1 function Person (Firstname,lastname,age,eyecolor) {2this. first=FirstName; 3 this. lastname=LastName; 4 this. age= age; 5 this. eyecolor=eyecol
After you create a JavaScript object, you can create an instance of the object:
1 var New person ("Bill", "Gates", "a", "Blue"); 2 var mymother=New person ("Steve", "Jobs", "green");
Add a method to a JavaScript object
1 functionPerson (Firstname,lastname,age,eyecolor) {2 This. firstname=FirstName;3 This. lastname=LastName;4 This. age=Age ;5 This. eyecolor=Eyecolor;6 7 This. changename=ChangeName;8 functionChangeName (name) {9 This. lastname=name;Ten } One}
JavaScript class
JavaScript classes are object-oriented languages, but JavaScript does not use classes
In JavaScript, classes are not created and objects are not created through classes
JavaScript is based on prototype (prototypes) instead of class-based
JavaScript for: In loop
For...in iterates through each property in the object
1 var person=={fname: "Bill", lname: "Gates", Age "" "; 2 3 For (x in person) 4 {5txt=txt+person[x]; 6 }7 document.getElementById ("Demo"). Innerhtml=txt;
The output will be:
Billgates56
Creating JavaScript objects