|
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 |
| 1 | +/* All Objects in JS have access to constructor |
| 2 | +property that returns a constructor function that created it |
| 3 | +// built in constructor functions |
| 4 | +// arrays and functions are objects in javascript |
| 5 | +*/ |
5 | 6 |
|
6 |
| -function person(firstName, lastName) { |
| 7 | +// create constructor function |
| 8 | +function MemberSHP(firstName, lastName) { |
| 9 | + // * use this for new instance & assign use param |
7 | 10 | this.firstName = firstName;
|
8 | 11 | this.lastName = lastName;
|
9 | 12 | this.fullName = function () {
|
10 |
| - console.log( |
11 |
| - `my full name is ${this.firstName} ${this.lastName}, i love JS` |
12 |
| - ); |
| 13 | + console.log(`Hello ${this.firstName} ${this.lastName}`); |
13 | 14 | };
|
| 15 | + // console.log(this); |
14 | 16 | }
|
15 | 17 |
|
16 |
| -// here example constructor function |
17 |
| -// *** obj, array, function |
| 18 | +// create blue print use NEW keyword |
| 19 | +const lutfy = new MemberSHP('mugiwara no', 'lutfy'); |
18 | 20 |
|
19 |
| -const john = new person('john', 'doe'); |
20 |
| -// console.log(john.constructor); |
| 21 | +// check constructor property |
| 22 | +console.log(lutfy.constructor); |
| 23 | +const thisArray = []; |
| 24 | +console.log(thisArray.constructor); |
| 25 | +const thisObj = {}; |
| 26 | +console.log(thisObj.constructor); |
| 27 | +const thisFunc = function () {}; |
| 28 | +console.log(thisFunc.constructor); |
21 | 29 |
|
22 |
| -const bob = {}; |
23 |
| -console.log(bob.constructor); |
24 |
| -const list = []; |
25 |
| -console.log(list.constructor); |
26 |
| -const peter = function () {}; |
27 |
| -console.log(peter); |
| 30 | +/* output: |
| 31 | +ƒ MemberSHP(firstName, lastName) { |
| 32 | + // * use this for new instance & assign use param |
| 33 | + this.firstName = firstName; |
| 34 | + this.lastName = lastName; |
| 35 | + this.fullName = function () { |
| 36 | + console.log(`Hello $… |
| 37 | +
|
| 38 | +ƒ Array() { [native code] } |
| 39 | +ƒ Object() { [native code] } |
| 40 | +ƒ Function() { [native code] } |
| 41 | +
|
| 42 | +*/ |
28 | 43 |
|
29 |
| -// example here can new instance will same constructor |
30 |
| -const jane = new john.constructor('jane', 'edward'); |
31 |
| -jane.fullName(); |
| 44 | +// dan sekarang kita bisa menggunakan constructor |
| 45 | +// dgn nilai yang sudah ada |
| 46 | +const zoro = new lutfy.constructor('rorona', 'zoro'); |
| 47 | +zoro.fullName(); //Hello rorona zoro |
0 commit comments