Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 32c8d5a

Browse files
committed
ES6 Class Syntax
1 parent 818acec commit 32c8d5a

File tree

2 files changed

+56
-21
lines changed

2 files changed

+56
-21
lines changed

‎app.js

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
11
/*
2-
Property Lookup
3-
If child does not have ask parent
4-
Everything in JS is an Object
2+
ES6 Classes - Syntactic Sugar
3+
Prototypal Inheritance
4+
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
58
*/
69

7-
function Account(name, initialBalance) {
8-
this.name = name;
9-
this.balance = initialBalance;
10-
this.bank = "Bank Of America";
11-
//This takes precedence over proto, if not present then it will look property and methods in proto
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+
}
1220
}
1321

14-
Account.prototype.bank = "CHASE";
15-
Account.prototype.deposit = function (amount) {
16-
this.balance = this.balance + amount;
17-
console.log(`Hello ${this.name}, your balance is ${this.balance}`);
18-
};
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";
1924

2025
const john = new Account("john", 200);
21-
const bob = new Account("bob", 400);
22-
23-
// john.deposit(300);
24-
// bob.deposit(1000);
25-
2626
console.log(john);
27-
console.log(bob.bank);
27+
console.log(john.name);
28+
console.log(john.bank);
29+
john.deposit(200);
2830

29-
console.log({});
30-
console.log([]);
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);

‎proto.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Property Lookup
3+
If child does not have ask parent
4+
Everything in JS is an Object
5+
*/
6+
7+
function Account(name, initialBalance) {
8+
this.name = name;
9+
this.balance = initialBalance;
10+
this.bank = "Bank Of America";
11+
//This takes precedence over proto, if not present then it will look property and methods in proto
12+
}
13+
14+
Account.prototype.bank = "CHASE";
15+
Account.prototype.deposit = function (amount) {
16+
this.balance = this.balance + amount;
17+
console.log(`Hello ${this.name}, your balance is ${this.balance}`);
18+
};
19+
20+
const john = new Account("john", 200);
21+
const bob = new Account("bob", 400);
22+
23+
// john.deposit(300);
24+
// bob.deposit(1000);
25+
26+
console.log(john);
27+
console.log(bob.bank);
28+
29+
console.log({});
30+
console.log([]);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /