Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Static Attributes | Static
Object-Oriented Programming in JavaScript
course content

Зміст курсу

Object-Oriented Programming in JavaScript

Object-Oriented Programming in JavaScript

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

Static Attributes

As you already know, each object can have a unique attribute value, like, for example, studentA has name Andrew and studentB has name William. But sometimes it makes sense to set some static value - the common for all instances of the class.

Let's add a attribute called ageOfMajority. Obviously, the value is 18 for all Students’ objects, and can't be different for different students. We’ll add a static keyword.

1234567891011121314
class Student{ name; age; static ageOfMajority = 18 constructor(name, age){ this.name = name; this.age = age; console.log('Student created'); } } student = new Student('Robert', 22); console.log(Student.ageOfMajority);
copy

To refer to the static attribute, use the name of the class, not the object, because it is a property of the class.

Inside non-static functions, you can refer static attribute only using class name, not this keyword. Look how we refer non-static age and static ageOfMajority in method drink():

123456789101112131415161718192021
class Student{ name; age; static ageOfMajority = 18; constructor(name, age){ this.name = name; this.age = age; console.log('Student created'); } drink(){ if (this.age < Student.ageOfMajority){ console.log('I am too young, only', this.age, 'yrs old.'); } else { console.log('I am drunk.'); } } } student = new Student('Robert', 22); student.drink();
copy

Завдання

Let's create function interview(), which assigns a student to the class A, if GPA >= 3.0, or class B if not. For this, define static attribute gpaLimit and assign it with 3.0, and inside interview() function compare it with student's GPA. Print message Assigned to the class A or Assigned to the class B.

Завдання

Let's create function interview(), which assigns a student to the class A, if GPA >= 3.0, or class B if not. For this, define static attribute gpaLimit and assign it with 3.0, and inside interview() function compare it with student's GPA. Print message Assigned to the class A or Assigned to the class B.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Секція 3. Розділ 1
toggle bottom row

Static Attributes

As you already know, each object can have a unique attribute value, like, for example, studentA has name Andrew and studentB has name William. But sometimes it makes sense to set some static value - the common for all instances of the class.

Let's add a attribute called ageOfMajority. Obviously, the value is 18 for all Students’ objects, and can't be different for different students. We’ll add a static keyword.

1234567891011121314
class Student{ name; age; static ageOfMajority = 18 constructor(name, age){ this.name = name; this.age = age; console.log('Student created'); } } student = new Student('Robert', 22); console.log(Student.ageOfMajority);
copy

To refer to the static attribute, use the name of the class, not the object, because it is a property of the class.

Inside non-static functions, you can refer static attribute only using class name, not this keyword. Look how we refer non-static age and static ageOfMajority in method drink():

123456789101112131415161718192021
class Student{ name; age; static ageOfMajority = 18; constructor(name, age){ this.name = name; this.age = age; console.log('Student created'); } drink(){ if (this.age < Student.ageOfMajority){ console.log('I am too young, only', this.age, 'yrs old.'); } else { console.log('I am drunk.'); } } } student = new Student('Robert', 22); student.drink();
copy

Завдання

Let's create function interview(), which assigns a student to the class A, if GPA >= 3.0, or class B if not. For this, define static attribute gpaLimit and assign it with 3.0, and inside interview() function compare it with student's GPA. Print message Assigned to the class A or Assigned to the class B.

Завдання

Let's create function interview(), which assigns a student to the class A, if GPA >= 3.0, or class B if not. For this, define static attribute gpaLimit and assign it with 3.0, and inside interview() function compare it with student's GPA. Print message Assigned to the class A or Assigned to the class B.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Секція 3. Розділ 1
toggle bottom row

Static Attributes

As you already know, each object can have a unique attribute value, like, for example, studentA has name Andrew and studentB has name William. But sometimes it makes sense to set some static value - the common for all instances of the class.

Let's add a attribute called ageOfMajority. Obviously, the value is 18 for all Students’ objects, and can't be different for different students. We’ll add a static keyword.

1234567891011121314
class Student{ name; age; static ageOfMajority = 18 constructor(name, age){ this.name = name; this.age = age; console.log('Student created'); } } student = new Student('Robert', 22); console.log(Student.ageOfMajority);
copy

To refer to the static attribute, use the name of the class, not the object, because it is a property of the class.

Inside non-static functions, you can refer static attribute only using class name, not this keyword. Look how we refer non-static age and static ageOfMajority in method drink():

123456789101112131415161718192021
class Student{ name; age; static ageOfMajority = 18; constructor(name, age){ this.name = name; this.age = age; console.log('Student created'); } drink(){ if (this.age < Student.ageOfMajority){ console.log('I am too young, only', this.age, 'yrs old.'); } else { console.log('I am drunk.'); } } } student = new Student('Robert', 22); student.drink();
copy

Завдання

Let's create function interview(), which assigns a student to the class A, if GPA >= 3.0, or class B if not. For this, define static attribute gpaLimit and assign it with 3.0, and inside interview() function compare it with student's GPA. Print message Assigned to the class A or Assigned to the class B.

Завдання

Let's create function interview(), which assigns a student to the class A, if GPA >= 3.0, or class B if not. For this, define static attribute gpaLimit and assign it with 3.0, and inside interview() function compare it with student's GPA. Print message Assigned to the class A or Assigned to the class B.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

As you already know, each object can have a unique attribute value, like, for example, studentA has name Andrew and studentB has name William. But sometimes it makes sense to set some static value - the common for all instances of the class.

Let's add a attribute called ageOfMajority. Obviously, the value is 18 for all Students’ objects, and can't be different for different students. We’ll add a static keyword.

1234567891011121314
class Student{ name; age; static ageOfMajority = 18 constructor(name, age){ this.name = name; this.age = age; console.log('Student created'); } } student = new Student('Robert', 22); console.log(Student.ageOfMajority);
copy

To refer to the static attribute, use the name of the class, not the object, because it is a property of the class.

Inside non-static functions, you can refer static attribute only using class name, not this keyword. Look how we refer non-static age and static ageOfMajority in method drink():

123456789101112131415161718192021
class Student{ name; age; static ageOfMajority = 18; constructor(name, age){ this.name = name; this.age = age; console.log('Student created'); } drink(){ if (this.age < Student.ageOfMajority){ console.log('I am too young, only', this.age, 'yrs old.'); } else { console.log('I am drunk.'); } } } student = new Student('Robert', 22); student.drink();
copy

Завдання

Let's create function interview(), which assigns a student to the class A, if GPA >= 3.0, or class B if not. For this, define static attribute gpaLimit and assign it with 3.0, and inside interview() function compare it with student's GPA. Print message Assigned to the class A or Assigned to the class B.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 3. Розділ 1
Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt