File tree Expand file tree Collapse file tree 1 file changed +27
-29
lines changed Expand file tree Collapse file tree 1 file changed +27
-29
lines changed Original file line number Diff line number Diff line change 1
- /*
2
- ES6 Classes - Syntactic Sugar
3
- Prototypal Inheritance
1
+ // call - runs instantly, arguments - list of items
4
2
5
- No keywords inside the class (bank, deposit(){}) apart from constructor
6
- Property(bank) are created on each instance
7
- Methods(deposit(){}) are on prototype
8
- */
3
+ const john = {
4
+ name : "john" ,
5
+ age : 25 ,
6
+ greet : function ( ) {
7
+ console . log ( this ) ;
8
+ console . log ( `Hi, my name is ${ this . name } and ${ this . age } ` ) ;
9
+ } ,
10
+ } ;
9
11
10
- class Account {
11
- constructor ( name , initialBalance ) {
12
- this . name = name ;
13
- this . balance = initialBalance ;
14
- }
15
- bank = "Chase" ; //This gets created on each instance
16
- deposit ( amount ) {
17
- this . balance = this . balance + amount ;
18
- console . log ( `Hello ${ this . name } , your balance is ${ this . balance } ` ) ;
19
- }
12
+ const susan = {
13
+ name : "susan" ,
14
+ age : 21 ,
15
+ } ;
16
+
17
+ function greet ( ) {
18
+ console . log ( this ) ;
19
+ console . log ( `Hi, my name ${ this . name } and I am ${ this . age } years old` ) ;
20
20
}
21
21
22
- // Again, if want the a property on prototype to prevent memory issues, copy of same in each instance
23
- // Account.prototype.bank = "Bank of America";
22
+ // this will fail
23
+ // susan.greet();
24
+ // greet();
25
+ const secondGreet = john . greet ;
26
+ // secondGreet();
24
27
25
- const john = new Account ( "john" , 200 ) ;
26
- console . log ( john ) ;
27
- console . log ( john . name ) ;
28
- console . log ( john . bank ) ;
29
- john . deposit ( 200 ) ;
28
+ greet . call ( john ) ;
29
+ greet . call ( susan ) ;
30
+ greet . call ( { name : "peter" , age : 30 } ) ;
30
31
31
- const bob = new Account ( "bob" , 0 ) ;
32
- console . log ( bob ) ;
33
- console . log ( bob . name ) ;
34
- console . log ( john . bank ) ;
35
- bob . deposit ( 1000 ) ;
32
+ // Making "this" point to susan object
33
+ john . greet . call ( susan ) ;
You can’t perform that action at this time.
0 commit comments