Skip to content

Commit 63fd49a

Browse files
authored
Add object examples
This commit adds object function property and property manipulation examples.
1 parent 7b84734 commit 63fd49a

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

docs/JavaScript_Basics/objects.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,47 @@ var person = {
3333
age:50,
3434
eyeColor:"blue"
3535
};
36-
```
36+
```
37+
38+
A function can be used as an object property:
39+
```JavaScript
40+
const objectWithFunction = {
41+
name: "Swapnil", // We can omit the " " in keys but for string values it is necessary
42+
rollno: 76, // We assign any type to keys
43+
getfull: function () { // Don't use arrow function here as arrow functions don't have this property
44+
console.log(`${this.name} ${this.rollno}`);
45+
}
46+
};
47+
48+
console.log(objectWithFunction);
49+
// Output { name: 'Swapnil', rollno: 76, getfull: [Function: getfull] }
50+
51+
objectWithFunction.getfull(); // Output Swapnil 76
52+
```
53+
54+
Object property values can be changed using dot notation:
55+
```JavaScript
56+
const a = {
57+
name: "Swapnil", // We can omit the " " in keys but for string values it is necessary
58+
rollno: 76 // We assign any type to keys
59+
};
60+
61+
a.name = "Swapnil Satish Shinde"; // This way we can change a particular property of object
62+
63+
console.log(a.name); // This way you can get the value of particular element
64+
// Output Swapnil Satish Shinde
65+
```
66+
67+
New properties can be added to an object after its creation:
68+
```JavaScript
69+
const canAddValue = { // This is normal object having 2 keys name and rollno
70+
name: "Swapnil",
71+
rollno: 76
72+
};
73+
74+
// We can add the keys we want any time into the object by directly assigning value to it
75+
canAddValue.branch = "Computer";
76+
77+
console.log(canAddValue);
78+
// Output { name: 'Swapnil', rollno: 76, branch: 'Computer' }
79+
```

0 commit comments

Comments
 (0)