|
| 1 | +//Object Methods |
| 2 | + |
| 3 | +/*In JavaScript, an "object" is a data structure that groups multiple values or properties and facilitates access to those properties. |
| 4 | +Objects contain named property-value pairs and can also contain methods such as functions. |
| 5 | +This forms the basis of object-oriented programming and allows you to keep data in a more organized and structured way.*/ |
| 6 | + |
| 7 | +//Object create a function in |
| 8 | +const person = { |
| 9 | + name: "John", |
| 10 | + greet: function () { |
| 11 | + console.log("hello"); |
| 12 | + }, |
| 13 | +}; |
| 14 | + |
| 15 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 16 | +//Object assigning value from outside |
| 17 | +let student = {}; |
| 18 | +student.name = "John"; |
| 19 | +student.greet = function () { |
| 20 | + console.log("hello"); |
| 21 | +}; |
| 22 | +student.greet(); // hello |
| 23 | + |
| 24 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 25 | +//delete |
| 26 | +//It allows us to delete a value inside the object. |
| 27 | +const user = { |
| 28 | + username: "AzureDiamond", |
| 29 | + password: "hunter2", |
| 30 | +}; |
| 31 | +delete user.username; |
| 32 | +console.log(user); // {"password": "hunter2"} |
| 33 | + |
| 34 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 35 | +//this keyword |
| 36 | +//We can use the this keyword to access a property of an object from within a method of the same object. |
| 37 | +const person1 = { |
| 38 | + name: "John", |
| 39 | + age: 30, |
| 40 | + |
| 41 | + // accessing name property by using this.name |
| 42 | + greet: function () { |
| 43 | + console.log("The name is" + " " + this.name); |
| 44 | + }, |
| 45 | +}; |
| 46 | +person1.greet(); |
| 47 | + |
| 48 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 49 | +//Also, when accessing values defined inside the function without using this, we use the this keyword to refer to the values inside the object. |
| 50 | +const person2 = { |
| 51 | + name: "John", |
| 52 | + surname: "Deep", |
| 53 | + greet: function () { |
| 54 | + let surname = "Doe"; |
| 55 | + console.log("The name is" + " " + this.name + " " + surname); |
| 56 | + }, |
| 57 | + greet2: function () { |
| 58 | + let surname = "Doe"; |
| 59 | + console.log("The name is" + " " + this.name + " " + this.surname); |
| 60 | + }, |
| 61 | +}; |
| 62 | +person2.greet(); // "The name is John Doe" |
| 63 | +person2.greet2(); // "The name is John Deep" |
| 64 | + |
| 65 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 66 | +//Object.keys |
| 67 | +//Creates an array containing the keys of an object. |
| 68 | +const employees = { |
| 69 | + boss: "Michael", |
| 70 | + secretary: "Pam", |
| 71 | + sales: "Jim", |
| 72 | + accountant: "Oscar", |
| 73 | +}; |
| 74 | +console.log(Object.keys(employees)); // ["boss", "secretary", "sales", "accountant"] |
| 75 | + |
| 76 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 77 | +// Object.values |
| 78 | +//Creates an array containing the values of an object. |
| 79 | +const session = { |
| 80 | + id: 1, |
| 81 | + time: `26-July-2018`, |
| 82 | + device: "mobile", |
| 83 | + browser: "Chrome", |
| 84 | +}; |
| 85 | +console.log(Object.values(session)); // [1, "26-July-2018", "mobile", "Chrome"] |
| 86 | + |
| 87 | +const operatingSystem = { |
| 88 | + name: "Ubuntu", |
| 89 | + version: 18.04, |
| 90 | + license: "Open Source", |
| 91 | +}; |
| 92 | + |
| 93 | +console.log(Object.entries(operatingSystem)); |
| 94 | +// output : |
| 95 | +`[["name", "Ubuntu"][("version", 18.04)][("license", "Open Source")]];` |
| 96 | + |
| 97 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 98 | +const operatingSystem1 = { |
| 99 | + name: "Ubuntu", |
| 100 | + version: 18.04, |
| 101 | + license: "Open Source", |
| 102 | +}; |
| 103 | +for (let [key, value] of Object.entries(operatingSystem1)) { |
| 104 | + console.log(key + " = " + value); |
| 105 | +} |
| 106 | +// output : |
| 107 | +("name = Ubuntu"); |
| 108 | +("version = 18.04"); |
| 109 | +("license = Open Source"); |
| 110 | + |
| 111 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 112 | +//Object.assign() |
| 113 | +//Used to combine values of multiple objects |
| 114 | +const name1 = { |
| 115 | + firstName: "Philip", |
| 116 | + lastName: "Fry", |
| 117 | +}; |
| 118 | + |
| 119 | +const details1 = { |
| 120 | + job: "Delivery Boy", |
| 121 | + employer: "Planet Express", |
| 122 | +}; |
| 123 | +console.log(Object.assign(name1, details1)); |
| 124 | +// output : |
| 125 | +`{firstName: "Philip", lastName: "Fry", job: "Delivery Boy", employer: "Planet Express"}`; |
| 126 | + |
| 127 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 128 | +//It is also possible to combine with the spread. |
| 129 | +const name2 = { |
| 130 | + firstName: "Philip", |
| 131 | + lastName: "Fry", |
| 132 | +}; |
| 133 | + |
| 134 | +const details = { |
| 135 | + job: "Delivery Boy", |
| 136 | + employer: "Planet Express", |
| 137 | +}; |
| 138 | +const character = { ...name2, ...details }; |
| 139 | + |
| 140 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 141 | +//Object.freeze() |
| 142 | +//Prevents changing, deleting and adding values in the specified object. |
| 143 | +const user2 = { |
| 144 | + username: "AzureDiamond", |
| 145 | + password: "hunter2", |
| 146 | +}; |
| 147 | + |
| 148 | +const newUser = Object.freeze(user2); |
| 149 | + |
| 150 | +newUser.password = "*******"; |
| 151 | +newUser.active = true; |
| 152 | +console.log(newUser); // {username: "AzureDiamond", password: "hunter2"} |
| 153 | + |
| 154 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 155 | +//Object.seal() |
| 156 | +//Allows the values in the specified object to be changed, but prevents adding and deleting values. |
| 157 | +const user3 = { |
| 158 | + username: "AzureDiamond", |
| 159 | + password: "hunter2", |
| 160 | +}; |
| 161 | + |
| 162 | +const newUser = Object.seal(user3); |
| 163 | +newUser.password = "*******"; |
| 164 | +newUser.active = true; //eklenmez |
| 165 | +delete newUser.username; //silinmez |
| 166 | +console.log(newUser); // {username: "AzureDiamond", password: "*******"} |
| 167 | + |
| 168 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 169 | +//shorthand |
| 170 | +//We can define your object methods in a more practical way without assigning keywords. |
| 171 | +// We assigned a function to greet and now we can call this function as greet. |
| 172 | +let person3 = { |
| 173 | + firstName: "John", |
| 174 | + lastName: "Doe", |
| 175 | + greet: function () { |
| 176 | + console.log("Hello, World!"); |
| 177 | + }, |
| 178 | +}; |
| 179 | +person3.greet(); // "Hello, World!"; |
| 180 | + |
| 181 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 182 | +// when we create the function directly without defining a keyword |
| 183 | +// The function name and keyword are automatically created inside |
| 184 | +let person5 = { |
| 185 | + firstName: "John", |
| 186 | + lastName: "Doe", |
| 187 | + greet() { |
| 188 | + console.log("Hello, World!"); |
| 189 | + }, |
| 190 | +}; |
| 191 | +person5.greet(); //Hello, World!" |
| 192 | + |
| 193 | +___________________________________________________________________________________________________________________________________________________________________________ |
| 194 | +//Object.getPrototypeOf() |
| 195 | +//Object.getPrototypeOf() is used to get the internal hidden [[Prototype]] of an object, also accessible through the __proto__ property. |
| 196 | +const employees1 = ["Ron", "April", "Andy", "Leslie"]; |
| 197 | +Object.getPrototypeOf(employees1); |
| 198 | +//Output |
| 199 | +//[constructor: ƒ, concat: ƒ, find: ƒ, findIndex: ƒ, pop: ƒ, …] |
| 200 | + |
0 commit comments