1+ const person = {
2+ firstName : "John" ,
3+ lastName : "Doe" ,
4+ age : 30 ,
5+ isStudent : false
6+ } ;
7+
8+ console . log ( person . firstName )
9+
10+ // class and object
11+
12+
13+ class client {
14+
15+ info ( ) {
16+ console . log ( "welcome to p4n.in" ) ;
17+ }
18+
19+ }
20+
21+ // call from object
22+
23+ obj = new client ( ) ;
24+ obj . info ( ) ;
25+
26+
27+ // object create method Object.create()
28+
29+
30+ const animal = {
31+ type : "Mammal" ,
32+ sound : "Roar"
33+ } ;
34+
35+ const lion = Object . create ( animal ) ;
36+ lion . name = "Simba" ;
37+
38+ console . log ( lion . name )
39+ console . log ( lion . type )
40+
41+
42+ // Object Methods
43+
44+ const calculator = {
45+ add : function ( a , b ) {
46+ return a + b ;
47+ } ,
48+ subtract : function ( a , b ) {
49+ return a - b ;
50+ }
51+ } ;
52+
53+ console . log ( calculator . add ( 5 , 3 ) ) ; // 8
54+ console . log ( calculator . subtract ( 8 , 2 ) ) ; // 6
55+
56+
57+ const p4n_calculator = {
58+ gst : function ( price , gst ) {
59+ finalprice = price * gst / 100
60+ finalprice = price + finalprice
61+ console . log ( "final price : " + finalprice )
62+ }
63+
64+ } ;
65+
66+ p4n_calculator . gst ( 1200 , 18 ) ;
67+
68+ // Object Properties and Methods
69+ const student = {
70+ firstName : "Alice" ,
71+ lastName : "Smith" ,
72+ age : 20 ,
73+ greet : function ( ) {
74+ console . log ( `Hello, I'm ${ this . firstName } ${ this . lastName } .` ) ;
75+ }
76+ } ;
77+
78+ student . greet ( ) ; // "Hello, I'm Alice Smith."
79+
80+ // Object Iteration
81+
82+ const person1 = {
83+ name : "John" ,
84+ age : 20 ,
85+ } ;
86+ console . log ( person1 ) ;
87+
88+ for ( let key in person1 ) {
89+ console . log ( key , person1 [ key ] ) ;
90+ }
91+
92+ person1 . age = 33 ;
93+
94+ for ( let key in person1 ) {
95+ console . log ( key , person1 [ key ] ) ;
96+ }
97+
98+ const keys = Object . keys ( person1 ) ;
99+ const values = Object . values ( person1 ) ;
100+ const entries = Object . entries ( person1 ) ;
0 commit comments