tubeshowcase●1,194,391 videos♡ My collection 0
← Learn to Code

Learn to Code08:04

Learn JavaScript STATIC keyword in 8 minutes! ⚡

Bro Code ↗

From the creator

// static = keyword that defines properties or methods that belong // to a class itself rather than the objects created // from that class (class owns anything static, not the objects) // ------------ EXAMPLE 1 ------------ class MathUtil{ static PI = 3.14159; static getDiameter(radius){ return radius * 2; } static getCircumference(radius){ return 2 * this.PI * radius; } static getArea(radius){ return this.PI * radius * radius; } } console.log(MathUtil.PI); console.log(MathUtil.getDiameter(10)); console.log(MathUtil.getCircumference(10)); console.log(MathUtil.getArea(10)); // ------------ EXAMPLE 2 ------------ class User{ static userCount = 0; constructor(username){ this.username = username; User.userCount++; } static getUserCount(){ console.log(`There are ${User.userCount} users online`); } sayHello(){ console.log(`Hello, my username is ${this.username}`); } } const user1 = new User("Spongebob"); const user2 = new User("Patrick"); const user3 = new User("Sandy"); user1.sayHello(); user2.sayHello(); user3.sayHello(); User.getUserCount();

Published 2023-11-18 · Watch on YouTube ↗
Availability and playback restrictions are controlled by the original channel.

Keep exploring Learn to Code