Day-8 Method, this

So Eun Ahn·2021년 1월 9일
0

JavaScript

목록 보기
12/26
post-custom-banner

Method: this is function that stored as property
Method actions that can be performed on objects
Method stored in properties as function definitions.
this key word : In a method, 'this' refers to the owner object.

const sso = {
    firstName: 'Sso',
    lastName: 'Ahn',
    birthYear: 1992,
    job: 'director',
    friends: ['Michael', 'Peter', 'Steven'],
    hasDriverLicense: true, //boolean property(data type),

    // function (birthYear) {
    //     return 2037 - birthYear;
    // } this would not work because it is only declaration object method need to be expression!!

    calcAge: function () {
    console.log(this);
    return 2037 - this.birthYear; //this.birthYear means the birthYear property of this object.

} //sso object that "owns" the calcAge function

정리하자면, method안의 value를 owner object에 property로 store하고 싶다면 this. keyword를 이용한다.
사용하기 전에은 해당 value는 ower object에서 property로 define/declare되어지지 않았기에 undefined value로 나타내진다.

code challenge 완료!!

Recap:

Objects can holds different typs of data
objects can hold object inside object (similarities with array)

profile
오늘도 새로운 도전을 한다
post-custom-banner

0개의 댓글