Constructor


Constructor
  • オブジェクトの複数のコピーおよびライトジェネレータ
  • Instance
  • コンストラクション関数によって新しく作成されたobject
  • 	// Constructor : makeStudent
    	// Instance : this
    	// (constructor 생성 시 일반 함수와는 다르게 맨 앞을 대문자로 만들어준다.)
    	function MakeStudent(name, age) {
                this.name = name;
                this.age = age;
                this.sayHi = () => {
                    console.log('안녕하세요 ' + this.name+ ' 입니다')
                }
            }
    
    	let student1 = new MakeStudent('Park', 20);
            console.log(student1)
            // makeStudent {name: 'Park', age: 20, sayHi: ƒ}
    
            student1.sayHi();
            // 안녕하세요 Park 입니다
            
            let student2 = new MakeStudent();
            console.log(student2)
            // makeStudent {name: undefined, age: undefined, sayHi: ƒ}
    
    使用例
  • 商品ごとに付加税()という内部関数が実行されている場合、コンソールウィンドウで商品価格*10%に等しい付加税金額を出力しますか?
  • 	function Product(상품명, 가격){
                this.name = 상품명;
                this.price = 가격;
                this.부가세 = () =>{
                    console.log(this.price * 0.1)
                }
            }
    
            let product = new Product('shirts', 50000);
           
            product.부가세()
            // 5000