Skip to content

Commit 093b607

Browse files
committed
process on call
1 parent fad55f0 commit 093b607

10 files changed

+276
-0
lines changed

01_object_basics.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const person = {
2+
name: 'john doe',
3+
age: 25,
4+
married: true,
5+
siblings: ['anna', 'peter'],
6+
greet: function (name) {
7+
return `hello, my name is ${name}`;
8+
},
9+
sayHello(name) {
10+
console.log(`hello, my name is ${name}`);
11+
},
12+
};
13+
14+
// acces obj u can use dot notation
15+
// console.log(person.name); //john doe
16+
// change name john doe be peter walker
17+
person.name = 'peter walker';
18+
// console.log(person.name);
19+
20+
// print all object
21+
// console.log(person);
22+
// access object with function parameter
23+
// console.log(person.greet('wiwa'));
24+
25+
//acces obj on array
26+
console.log(person.siblings[person.siblings.length - 1]);
27+
28+
// delete property
29+
let siblings = delete person.siblings;
30+
console.log(siblings); //true its succesfully for delete
31+
console.log(person);

02_nested_obj.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const newnames = 'apple.inc';
2+
const age = 20;
3+
let random = 'random-value';
4+
5+
const person = {
6+
name: 'john doe',
7+
age: age,
8+
company: {
9+
name: newnames,
10+
job: 'front end developer',
11+
},
12+
married: true,
13+
expert: {
14+
skills: ['html', 'css3', 'js'],
15+
hobby: {
16+
books: ['onepiece', 'shokugeki no souma'],
17+
},
18+
},
19+
'random-value': 'jobss!',
20+
};
21+
22+
// acces all obj
23+
// console.log(person);
24+
25+
// *** acces on nested
26+
// front end developer
27+
console.log(person.company.job);
28+
// [ 'html', 'css3', 'js' ]
29+
console.log(person.expert.skills);
30+
// shokugeki no souma
31+
console.log(person.expert.hobby.books[1]);
32+
console.log('random-value');
33+
// apple.inc
34+
console.log(person.company.name);
35+
console.log(person['random-value']);
36+
// { name: 'apple.inc', job: 'front end developer' }
37+
// still same with person.company
38+
console.log(person['company']);
39+
40+
console.log(person.age);

03_this_basics.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const shp_list = {
2+
firstName: 'mugiwara no',
3+
lastName: 'luffy',
4+
sayToHello: function () {
5+
console.log(`Hello captain ${this.firstName} ${this.lastName}`);
6+
},
7+
};
8+
9+
// this keyword get value object dari dalam object itu sndiri
10+
shp_list.sayToHello();

04_factory_function.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// blue print
2+
// factory function and constructor functions
3+
// factory functions for return obj & based on parameter
4+
//
5+
6+
// *** remember heres not dynamic
7+
// here if add text i love js for john
8+
// its good but how for bobby, or 3000 obj with name?
9+
// for solution you need factory functions
10+
11+
// const john = {
12+
// firstName: 'john',
13+
// lastName: 'doe',
14+
// fullName: function () {
15+
// console.log(`my full name is ${this.firstName} ${this.lastName}
16+
// i love js`);
17+
// },
18+
// };
19+
20+
// const bob = {
21+
// firstName: 'bob',
22+
// lastName: 'walker',
23+
// fullName: function () {
24+
// console.log(`my full name is ${this.firstName} ${this.lastName}`);
25+
// },
26+
// };
27+
28+
// ***** Factory Function
29+
function createPerson(firstName, lastName) {
30+
return {
31+
firstName: firstName,
32+
lastName: lastName,
33+
fullName: function () {
34+
console.log(`My name is ${this.firstName} ${this.lastName} i love JS`);
35+
},
36+
};
37+
}
38+
39+
const john = createPerson('john', 'doe');
40+
john.fullName();
41+
const bob = createPerson('bob', 'walker');
42+
bob.fullName();
43+
const susy = createPerson('susy', 'kane');
44+
susy.fullName();
45+
const kelly = createPerson('kelly', 'jane');
46+
kelly.fullName();

05_constructor_function.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// constructor function
2+
// new - creates new object, points to it, omit return
3+
function person(firstName, lastName) {
4+
this.firstName = firstName;
5+
this.lastName = lastName;
6+
this.fullName = function () {
7+
console.log(`my full name is ${this.firstName} ${this.lastName}`);
8+
};
9+
console.log(this);
10+
}
11+
12+
// access obj use new
13+
14+
const john = new person('john', 'doe');
15+
john.fullName();
16+
const boby = new person('boby', 'walker');
17+
boby.fullName();
18+
const susy = new person('susy', 'dane');
19+
susy.fullName();

06_constructor_property.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// all objects in JS have access to constructor
2+
// property that returuns a constructor function that created it
3+
// built in constructor function
4+
// arrays and functions are object in JS
5+
6+
function person(firstName, lastName) {
7+
this.firstName = firstName;
8+
this.lastName = lastName;
9+
this.fullName = function () {
10+
console.log(
11+
`my full name is ${this.firstName} ${this.lastName}, i love JS`
12+
);
13+
};
14+
}
15+
16+
// here example constructor function
17+
// *** obj, array, function
18+
19+
const john = new person('john', 'doe');
20+
// console.log(john.constructor);
21+
22+
const bob = {};
23+
console.log(bob.constructor);
24+
const list = [];
25+
console.log(list.constructor);
26+
const peter = function () {};
27+
console.log(peter);
28+
29+
// example here can new instance will same constructor
30+
const jane = new john.constructor('jane', 'edward');
31+
jane.fullName();

07_prototype_property.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function Banking(fullName, initialBalance) {
2+
this.name = fullName;
3+
this.balance = initialBalance;
4+
// function
5+
}
6+
7+
// here you can create New prototype
8+
Banking.prototype.bank = 'Mandiri';
9+
Banking.prototype.deposit = function (amount) {
10+
this.balance += amount;
11+
console.log(
12+
`Hello ${this.name} your balance on the bank ${this.bank} for now is ${this.balance}`
13+
);
14+
};
15+
const kelly = new Banking('kelly', 2000);
16+
const jane = new Banking('jane', 0);
17+
18+
console.log(kelly);
19+
console.log(jane);
20+
kelly.deposit(2200);
21+
jane.deposit(10560000);
22+
23+
// output:
24+
// Hello kelly your balance on the bank Mandiri for now is 4200
25+
// Hello jane your balance on the bank Mandiri for now is 10560000

08_ES6_Class_syntax.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// ES6
2+
// Basic constructor 01-07 chapter
3+
4+
class Account {
5+
// constructor
6+
constructor(fullName, initialBalance) {
7+
this.name = fullName;
8+
this.balance = initialBalance;
9+
}
10+
// prototype here will input to constructor
11+
bank = 'Mandiri Indonesia';
12+
// function
13+
deposite(amount) {
14+
this.balance += amount;
15+
console.log(
16+
`Hello ${this.name} your balance on ${this.bank} is Rp. ${this.balance} `
17+
);
18+
}
19+
}
20+
21+
// field object don't forget create new
22+
const kelly = new Account('kelly jane', 20000);
23+
console.log(kelly);
24+
kelly.deposite(40000);
25+
// output: Hello kelly jane your balance on Mandiri Indonesia is Rp. 60000
26+
27+
const jenny = new Account('jenny edward', 15000);
28+
jenny.deposite(15000);
29+
// Hello jenny edward your balance on Mandiri Indonesia is Rp. 30000

09.call.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// call method,
2+
// you can write a method that can be used on different objects.
3+
4+
const john = {
5+
name: 'john',
6+
age: 25,
7+
greeting: function () {
8+
console.log(this);
9+
console.log(`My name is ${this.name} and my age is ${this.age}`);
10+
},
11+
};
12+
13+
const susy = {
14+
name: 'susy',
15+
age: 30,
16+
};
17+
18+
// u can use two option: like function this
19+
// or use object
20+
function greeting() {
21+
console.log(this);
22+
console.log(`Your name is ${this.name} and age is ${this.age}`);
23+
}
24+
25+
greeting.call(susy);
26+
// Your name is susy and age is 30
27+
28+
// or you can use this one
29+
john.greeting.call(susy);
30+
// My name is susy and my age is 30
31+
32+
// and for create again use argument
33+
john.greeting.call({ name: 'peter walker', age: 68 });

index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
<h2>constructor function</h2>
10+
</body>
11+
<script src="./09.call.js"></script>
12+
</html>

0 commit comments

Comments
 (0)