|
| 1 | +# Objects in JavaScript |
| 2 | + |
| 3 | +An object in JavaScript is a collection of key-value pairs. Each key-value pair is called as a property. A property can be a function, an array, an object itself or any primitive data type i.e. integer, string, etc. Functions in object are called as methods. |
| 4 | + |
| 5 | +Here firstName, lastName, and fullName are properties of the same object i.e. human. firstName is the key and Virat is the value of the property. |
| 6 | + |
| 7 | +```JavaScript |
| 8 | +var human = { |
| 9 | + firstName: "Virat", |
| 10 | + lastName: "Kohli", |
| 11 | + age: 30, |
| 12 | + fullName: function(){ |
| 13 | + return this.firstName + " " + this.lastName |
| 14 | + }, |
| 15 | + otherInformation: { |
| 16 | + adress:{ |
| 17 | + city: 'Mumbai', |
| 18 | + street: 'street 1' |
| 19 | + }, |
| 20 | + phone: '+91-312452345' |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +console.log(human.fullName()); //Virat Kohli |
| 25 | +``` |
| 26 | + |
| 27 | +**Properties of the object can be accessed using** |
| 28 | + |
| 29 | +1. Dot notation. |
| 30 | + |
| 31 | + e.g. human.fullName(), human.firstName, human.otherInformation.adress |
| 32 | + |
| 33 | +2. Square bracket |
| 34 | + |
| 35 | + e.g. |
| 36 | + |
| 37 | + ``` |
| 38 | + human["firstName"]; //Output: Virat |
| 39 | + human["fullName"](); //Output: Virat Kohli |
| 40 | + ``` |
| 41 | + |
| 42 | + ``` |
| 43 | + var firstNameProperty = "firstName"; |
| 44 | + console.log(human[firstNameProperty]) // Output: Virat |
| 45 | + ``` |
| 46 | + |
| 47 | + **Note**: Above method of using variable to access property names cannot be used to access properties of the object using dot notation. |
| 48 | + |
| 49 | + ``` |
| 50 | + Console.log(human.firstNameProperty) //Output: undefined |
| 51 | + ``` |
| 52 | + |
| 53 | +New properties can be added using the dot notation as shown below: |
| 54 | + |
| 55 | +```JavaScript |
| 56 | +human.age = 27 |
| 57 | +human.getAge = function(){ |
| 58 | + return this.age; |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +An object property name can be any valid JavaScript string, or anything that can be converted to a string, including the empty string. |
| 63 | + |
| 64 | +**However**, any property name that is not a valid Javascript identifier (for example, a property name that has a space or a hyphen, or that starts with a number) can only be accessed and added to the object property using the square bracket notation |
| 65 | + |
| 66 | +```JavaScript |
| 67 | +human["date of birth"] = "Nov 28"; |
| 68 | +human[12] = 12; |
| 69 | +human.12 = 12; //gives error |
| 70 | + |
| 71 | +console.log(human.12); //Gives error |
| 72 | +console.log(human[12]); //Output: 12 |
| 73 | +``` |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +## Delete a property from an object |
| 78 | + |
| 79 | +To delete a property from an object we can use the delete operator. You cannot delete properties that were inherited, nor can you delete properties with their attributes set to configurable. |
| 80 | + |
| 81 | +‘delete’ operator returns true if the delete was successful. It also return true if the property to delete was non-existent or the property could not be deleted. |
| 82 | + |
| 83 | +``` |
| 84 | +delete human.firstName; // return true |
| 85 | +``` |
| 86 | + |
| 87 | +Let’s see what happens if we try to call fullName method which uses both the firstName and lastName property of human object. |
| 88 | + |
| 89 | +`console.log(human.fullName());// undefined Kohli` |
| 90 | + |
| 91 | +Output is undefined because we were trying to access firstName property of human object which does not exists. |
0 commit comments