Keyword 'this'
You can refer to the attributes not only outside the class but inside, too, for example, inside methods. To do that, use this
keyword. this
is like a name of object, but it means the current object, i. e. it refers to the global object.
Let's change current age of the student each time we call happyBirthday()
function, and print it:
123456789101112131415class Student{ name = 'June'; university = 'MIT'; age = 18; gpa; sleep(){ console.log('I am sleeping right now!') } happyBirthday(){ this.age += 1; console.log('Today is my birthday! I am', this.age, 'yrs old!'); } }
Swipe to start coding
Modify happyBirthday()
function: output info about new age and about name of the student. After that, create an object of class Student
, and call this method three times to see what happens with age.
Oplossing
Bedankt voor je feedback!
single
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Awesome!
Completion rate improved to 7.69Awesome!
Completion rate improved to 7.69
Keyword 'this'
You can refer to the attributes not only outside the class but inside, too, for example, inside methods. To do that, use this
keyword. this
is like a name of object, but it means the current object, i. e. it refers to the global object.
Let's change current age of the student each time we call happyBirthday()
function, and print it:
123456789101112131415class Student{ name = 'June'; university = 'MIT'; age = 18; gpa; sleep(){ console.log('I am sleeping right now!') } happyBirthday(){ this.age += 1; console.log('Today is my birthday! I am', this.age, 'yrs old!'); } }
Swipe to start coding
Modify happyBirthday()
function: output info about new age and about name of the student. After that, create an object of class Student
, and call this method three times to see what happens with age.
Oplossing
Bedankt voor je feedback!
single
Awesome!
Completion rate improved to 7.69
Keyword 'this'
Veeg om het menu te tonen
You can refer to the attributes not only outside the class but inside, too, for example, inside methods. To do that, use this
keyword. this
is like a name of object, but it means the current object, i. e. it refers to the global object.
Let's change current age of the student each time we call happyBirthday()
function, and print it:
123456789101112131415class Student{ name = 'June'; university = 'MIT'; age = 18; gpa; sleep(){ console.log('I am sleeping right now!') } happyBirthday(){ this.age += 1; console.log('Today is my birthday! I am', this.age, 'yrs old!'); } }
Swipe to start coding
Modify happyBirthday()
function: output info about new age and about name of the student. After that, create an object of class Student
, and call this method three times to see what happens with age.
Oplossing
Bedankt voor je feedback!