Skip to content

Instantly share code, notes, and snippets.

@jotaelesalinas
Created November 20, 2021 00:02
Show Gist options
  • Save jotaelesalinas/add88e678fa09bab33a5d0b503fe0856 to your computer and use it in GitHub Desktop.
Save jotaelesalinas/add88e678fa09bab33a5d0b503fe0856 to your computer and use it in GitHub Desktop.
Display different ways of accessing static members from normal methods and other static methods in Javascript classes
"use strict";
// methods ending in 1 are inherited from Base
// methods ending in 2 are overriden in Child
class Base {
static num = 123;
static str = "asdf";
fnInstanceThis1() {
console.log("----- Base.fnInstanceThis1() -----");
try { console.log("this.name:", this.name); } catch(err) { }
try { console.log("this.num:", this.num); } catch(err) { }
try { console.log("this.str:", this.str); } catch(err) { }
try { console.log("this.constructor.name:", this.constructor.name); } catch(err) { }
try { console.log("this.constructor.num:", this.constructor.num); } catch(err) { }
try { console.log("this.constructor.str:", this.constructor.str); } catch(err) { }
}
fnInstanceThis2() {
console.log("----- Base.fnInstanceThis2() -----");
try { console.log("this.name:", this.name); } catch(err) { }
try { console.log("this.num:", this.num); } catch(err) { }
try { console.log("this.str:", this.str); } catch(err) { }
try { console.log("this.constructor.name:", this.constructor.name); } catch(err) { }
try { console.log("this.constructor.num:", this.constructor.num); } catch(err) { }
try { console.log("this.constructor.str:", this.constructor.str); } catch(err) { }
}
static fnStaticThis1() {
console.log("----- Base.fnStaticThis1() -----");
try { console.log("this.name:", this.name); } catch(err) { }
try { console.log("this.num:", this.num); } catch(err) { }
try { console.log("this.str:", this.str); } catch(err) { }
try { console.log("this.constructor.name:", this.constructor.name); } catch(err) { }
try { console.log("this.constructor.num:", this.constructor.num); } catch(err) { }
try { console.log("this.constructor.str:", this.constructor.str); } catch(err) { }
}
static fnStaticThis2() {
console.log("----- Base.fnStaticThis2() -----");
try { console.log("this.name:", this.name); } catch(err) { }
try { console.log("this.num:", this.num); } catch(err) { }
try { console.log("this.str:", this.str); } catch(err) { }
try { console.log("this.constructor.name:", this.constructor.name); } catch(err) { }
try { console.log("this.constructor.num:", this.constructor.num); } catch(err) { }
try { console.log("this.constructor.str:", this.constructor.str); } catch(err) { }
}
static fnStaticClass1() {
console.log("----- Base.fnStaticClass1() -----");
try { console.log("Base.name:", Base.name); } catch(err) { }
try { console.log("Base.num:", Base.num); } catch(err) { }
try { console.log("Base.str:", Base.str); } catch(err) { }
try { console.log("Base.constructor.name:", Base.constructor.name); } catch(err) { }
try { console.log("Base.constructor.num:", Base.constructor.num); } catch(err) { }
try { console.log("Base.constructor.str:", Base.constructor.str); } catch(err) { }
}
static fnStaticClass2() {
console.log("----- Base.fnStaticClass1() -----");
try { console.log("Base.name:", Base.name); } catch(err) { }
try { console.log("Base.num:", Base.num); } catch(err) { }
try { console.log("Base.str:", Base.str); } catch(err) { }
try { console.log("Base.constructor.name:", Base.constructor.name); } catch(err) { }
try { console.log("Base.constructor.num:", Base.constructor.num); } catch(err) { }
try { console.log("Base.constructor.str:", Base.constructor.str); } catch(err) { }
}
}
class Child extends Base {
static str = "qwer";
fnInstanceThis2() {
console.log("----- Child.fnInstanceThis2() -----");
try { console.log("this.name:", this.name); } catch(err) { }
try { console.log("this.num:", this.num); } catch(err) { }
try { console.log("this.str:", this.str); } catch(err) { }
try { console.log("this.constructor.name:", this.constructor.name); } catch(err) { }
try { console.log("this.constructor.num:", this.constructor.num); } catch(err) { }
try { console.log("this.constructor.str:", this.constructor.str); } catch(err) { }
}
static fnStaticThis2() {
console.log("----- Child.fnStaticThis2() -----");
try { console.log("this.name:", this.name); } catch(err) { }
try { console.log("this.num:", this.num); } catch(err) { }
try { console.log("this.str:", this.str); } catch(err) { }
try { console.log("this.constructor.name:", this.constructor.name); } catch(err) { }
try { console.log("this.constructor.num:", this.constructor.num); } catch(err) { }
try { console.log("this.constructor.str:", this.constructor.str); } catch(err) { }
}
static fnStaticClass2() {
console.log("----- Child.fnStaticClass1() -----");
try { console.log("Child.name:", Child.name); } catch(err) { }
try { console.log("Child.num:", Child.num); } catch(err) { }
try { console.log("Child.str:", Child.str); } catch(err) { }
try { console.log("Child.constructor.name:", Child.constructor.name); } catch(err) { }
try { console.log("Child.constructor.num:", Child.constructor.num); } catch(err) { }
try { console.log("Child.constructor.str:", Child.constructor.str); } catch(err) { }
}
}
// we only call instance and static methods from Child
let v = new Child;
v.fnInstanceThis1();
v.fnInstanceThis2();
Child.fnStaticThis1();
Child.fnStaticThis2();
Child.fnStaticClass1();
Child.fnStaticClass2();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment