Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Keyword 'this' | Classes & Objects
Object-Oriented Programming in JavaScript
course content

Conteúdo do Curso

Object-Oriented Programming in JavaScript

Object-Oriented Programming in JavaScript

1. Classes & Objects
2. Encapsulation
3. Static
4. Inheritance

book
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:

123456789101112131415
class 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!'); } }
copy
Tarefa
test

Swipe to show code editor

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.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 5
toggle bottom row

book
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:

123456789101112131415
class 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!'); } }
copy
Tarefa
test

Swipe to show code editor

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.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 5
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt