Day 6 "Object"

Hector·2023년 3월 24일
0

Vanilla JavaScript

목록 보기
6/14
<script>
function Me(firstName,lastName){
    this.firstName = firstName;
    this.lastName = lastName;
 }
 
 Me.prototype.getFullName = function(){
    return `${this.firstName} ${this.lastName}`
 }


// new : (constructor(생성자) call(호출)

// An instance is created when the constructor is called (stores data in memory)
// 생성자가 호출되면 인스턴스가 생성됨( 메모리에 데이터 저장)

// instance: actual chunk of data
// 인스턴스 : 실제 데이터 덩어리
 let Ah = new Me('Ahn','Hector');



console.log(Ah);
console.log(Ah['getFullName']());
console.log(Ah.getFullName());
</script>

or

<script>
let me = {
    firstName: 'Ahn',
    lastName: 'Hector',
    age: 30,
    getFullName : function (){
        // When this is defined, it is called(this가 정의 되는 시점은 호출될때)
        return `${this.firstName} ${this.lastName}`;
    }
}

let you = {
    firstName: 'White',
    lastName: 'Snow',
    age: 1,
    getFullName : () => {
        // When this is defined inside a function(this가 정의 되는 시점은 함수 내부)
        return `${this.firstName} ${this.lastName}`;
    }
}
console.log(me);
console.log(me.firstName);
console.log(me['firstName'])
console.log(me.lastName);
console.log(me['lastName'])
console.log(me.age);
console.log(me['age']);
console.log(me.getFullName());
console.log(me['getFullName']());
</script>
profile
I`m Studying Bankend

0개의 댓글