In ES6, given the following example:
export default class MyStyle extends Stylesheet {
static Color = {
mainDark: '#000'
}
static Comp = {
...
color: Color.mainDark
}
}
How can I access Color.mainDark (the static field)?
In ES6, given the following example:
export default class MyStyle extends Stylesheet {
static Color = {
mainDark: '#000'
}
static Comp = {
...
color: Color.mainDark
}
}
How can I access Color.mainDark (the static field)?
Share Improve this question edited Sep 30, 2017 at 17:49 Felix Kling 817k180 gold badges1.1k silver badges1.2k bronze badges asked Dec 11, 2015 at 9:28 LiviosoLivioso 1,2521 gold badge12 silver badges23 bronze badges 6 | Show 1 more comment3 Answers
Reset to default 12You can access it as you would expect, however if I recall there were some issues when using Babel and exporting the class immediately, so export after defining the class if you're having problems:
class MyStyle extends Stylesheet {
static Color = {
mainDark: '#000'
}
someMethod() {
console.log(MyStyle.Color.mainDark);
}
}
export default MyStyle;
You can read more about the Babel issue in an answer Marian made on a similar question, which is supposedly fixed in Babel 6.2.1.
indeed, this.constructor points to the class hierarchy.
take a look:
class Some {
static static_elem = 'elem';
constructor(param) {
console.log( Some.static_elem, param);
console.log( this.constructor.static_elem, param);
}
};
var some = new Some('some');
class SomeOther extends Some {
constructor(param) {
super(param);
console.log( Some.static_elem, param);
console.log( SomeOther.static_elem, param);
console.log( this.constructor.static_elem, param);
}
};
var someother = new SomeOther('someother');
'use strict';
class User {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
static createGuest() {
return new User("guest", "site");
}
};
let user = User.createGuest();
alert( user.firstName ); // guest
alert( User.createGuest ); // createGuest ... (function)
And const:
'use strict';
class Menu {
static get elemClass() {
return "menu"
}
}
alert( Menu.elemClass ); // menu
use call to context object --- this
ES6 - Call static method within a class
}
on the last line – zerkms Commented Dec 11, 2015 at 9:32MyStyle.Color.mainDark
. – klaemo Commented Dec 11, 2015 at 9:33