Can a instance method in a Javascript subclass call it's parents static method?
up vote
0
down vote
favorite
This doesn't work. Is there a way for a child class in JS to have access to its parents static methods?
class Person {
constructor() {}
static isHuman() {
return 'yes I am';
}
}
class Brian extends Person {
constructor() {
super();
}
greeting() {
console.log(super.isHuman())
}
}
const b = new Brian();
b.greeting();
javascript
add a comment |
up vote
0
down vote
favorite
This doesn't work. Is there a way for a child class in JS to have access to its parents static methods?
class Person {
constructor() {}
static isHuman() {
return 'yes I am';
}
}
class Brian extends Person {
constructor() {
super();
}
greeting() {
console.log(super.isHuman())
}
}
const b = new Brian();
b.greeting();
javascript
No, is not possible. It doesn't make sense. You need to understand how the static members work. read more about it here
– Ele
Nov 11 at 0:59
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This doesn't work. Is there a way for a child class in JS to have access to its parents static methods?
class Person {
constructor() {}
static isHuman() {
return 'yes I am';
}
}
class Brian extends Person {
constructor() {
super();
}
greeting() {
console.log(super.isHuman())
}
}
const b = new Brian();
b.greeting();
javascript
This doesn't work. Is there a way for a child class in JS to have access to its parents static methods?
class Person {
constructor() {}
static isHuman() {
return 'yes I am';
}
}
class Brian extends Person {
constructor() {
super();
}
greeting() {
console.log(super.isHuman())
}
}
const b = new Brian();
b.greeting();
javascript
javascript
asked Nov 11 at 0:56
GN.
7331640
7331640
No, is not possible. It doesn't make sense. You need to understand how the static members work. read more about it here
– Ele
Nov 11 at 0:59
add a comment |
No, is not possible. It doesn't make sense. You need to understand how the static members work. read more about it here
– Ele
Nov 11 at 0:59
No, is not possible. It doesn't make sense. You need to understand how the static members work. read more about it here
– Ele
Nov 11 at 0:59
No, is not possible. It doesn't make sense. You need to understand how the static members work. read more about it here
– Ele
Nov 11 at 0:59
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
Yes you can. You can use super
but to get at the static method you have to access the constructor property of it:
super.constructor.isHuman()
You can also use the class name directly:
Person.isHuman();
//this works because Brian inherits the static methods from Person
Brian.isHuman();
Or by going up the prototype chain
//would be referencing Brian
this.constructor.isHuman();
//would be referencing Person
this.__proto__.constructor.isHuman();
Object.getPrototypeOf(this).constructor.isHuman();
Demo
class Person {
constructor() {}
static isHuman() {
return 'yes I am';
}
}
class Brian extends Person {
constructor() {
super();
}
greeting() {
console.log("From super: ", super.constructor.isHuman())
console.log("From class name (Brian): ", Brian.isHuman())
console.log("From class name (Person): ", Person.isHuman())
console.log("From prototype chain: ", this.constructor.isHuman())
console.log("From prototype chain: ", Object.getPrototypeOf(this).constructor.isHuman())
}
}
const b = new Brian();
b.greeting();
Awesome! Thanks
– GN.
Nov 11 at 23:28
add a comment |
up vote
0
down vote
For static methods, use the class name rather than super.
class Person {
constructor() {}
static isHuman() {
return 'yes I am';
}
}
class Brian extends Person {
constructor() {
super();
}
greeting() {
console.log(Person.isHuman())
}
}
const b = new Brian();
b.greeting();
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Yes you can. You can use super
but to get at the static method you have to access the constructor property of it:
super.constructor.isHuman()
You can also use the class name directly:
Person.isHuman();
//this works because Brian inherits the static methods from Person
Brian.isHuman();
Or by going up the prototype chain
//would be referencing Brian
this.constructor.isHuman();
//would be referencing Person
this.__proto__.constructor.isHuman();
Object.getPrototypeOf(this).constructor.isHuman();
Demo
class Person {
constructor() {}
static isHuman() {
return 'yes I am';
}
}
class Brian extends Person {
constructor() {
super();
}
greeting() {
console.log("From super: ", super.constructor.isHuman())
console.log("From class name (Brian): ", Brian.isHuman())
console.log("From class name (Person): ", Person.isHuman())
console.log("From prototype chain: ", this.constructor.isHuman())
console.log("From prototype chain: ", Object.getPrototypeOf(this).constructor.isHuman())
}
}
const b = new Brian();
b.greeting();
Awesome! Thanks
– GN.
Nov 11 at 23:28
add a comment |
up vote
0
down vote
accepted
Yes you can. You can use super
but to get at the static method you have to access the constructor property of it:
super.constructor.isHuman()
You can also use the class name directly:
Person.isHuman();
//this works because Brian inherits the static methods from Person
Brian.isHuman();
Or by going up the prototype chain
//would be referencing Brian
this.constructor.isHuman();
//would be referencing Person
this.__proto__.constructor.isHuman();
Object.getPrototypeOf(this).constructor.isHuman();
Demo
class Person {
constructor() {}
static isHuman() {
return 'yes I am';
}
}
class Brian extends Person {
constructor() {
super();
}
greeting() {
console.log("From super: ", super.constructor.isHuman())
console.log("From class name (Brian): ", Brian.isHuman())
console.log("From class name (Person): ", Person.isHuman())
console.log("From prototype chain: ", this.constructor.isHuman())
console.log("From prototype chain: ", Object.getPrototypeOf(this).constructor.isHuman())
}
}
const b = new Brian();
b.greeting();
Awesome! Thanks
– GN.
Nov 11 at 23:28
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Yes you can. You can use super
but to get at the static method you have to access the constructor property of it:
super.constructor.isHuman()
You can also use the class name directly:
Person.isHuman();
//this works because Brian inherits the static methods from Person
Brian.isHuman();
Or by going up the prototype chain
//would be referencing Brian
this.constructor.isHuman();
//would be referencing Person
this.__proto__.constructor.isHuman();
Object.getPrototypeOf(this).constructor.isHuman();
Demo
class Person {
constructor() {}
static isHuman() {
return 'yes I am';
}
}
class Brian extends Person {
constructor() {
super();
}
greeting() {
console.log("From super: ", super.constructor.isHuman())
console.log("From class name (Brian): ", Brian.isHuman())
console.log("From class name (Person): ", Person.isHuman())
console.log("From prototype chain: ", this.constructor.isHuman())
console.log("From prototype chain: ", Object.getPrototypeOf(this).constructor.isHuman())
}
}
const b = new Brian();
b.greeting();
Yes you can. You can use super
but to get at the static method you have to access the constructor property of it:
super.constructor.isHuman()
You can also use the class name directly:
Person.isHuman();
//this works because Brian inherits the static methods from Person
Brian.isHuman();
Or by going up the prototype chain
//would be referencing Brian
this.constructor.isHuman();
//would be referencing Person
this.__proto__.constructor.isHuman();
Object.getPrototypeOf(this).constructor.isHuman();
Demo
class Person {
constructor() {}
static isHuman() {
return 'yes I am';
}
}
class Brian extends Person {
constructor() {
super();
}
greeting() {
console.log("From super: ", super.constructor.isHuman())
console.log("From class name (Brian): ", Brian.isHuman())
console.log("From class name (Person): ", Person.isHuman())
console.log("From prototype chain: ", this.constructor.isHuman())
console.log("From prototype chain: ", Object.getPrototypeOf(this).constructor.isHuman())
}
}
const b = new Brian();
b.greeting();
class Person {
constructor() {}
static isHuman() {
return 'yes I am';
}
}
class Brian extends Person {
constructor() {
super();
}
greeting() {
console.log("From super: ", super.constructor.isHuman())
console.log("From class name (Brian): ", Brian.isHuman())
console.log("From class name (Person): ", Person.isHuman())
console.log("From prototype chain: ", this.constructor.isHuman())
console.log("From prototype chain: ", Object.getPrototypeOf(this).constructor.isHuman())
}
}
const b = new Brian();
b.greeting();
class Person {
constructor() {}
static isHuman() {
return 'yes I am';
}
}
class Brian extends Person {
constructor() {
super();
}
greeting() {
console.log("From super: ", super.constructor.isHuman())
console.log("From class name (Brian): ", Brian.isHuman())
console.log("From class name (Person): ", Person.isHuman())
console.log("From prototype chain: ", this.constructor.isHuman())
console.log("From prototype chain: ", Object.getPrototypeOf(this).constructor.isHuman())
}
}
const b = new Brian();
b.greeting();
edited Nov 11 at 1:18
answered Nov 11 at 1:12
Patrick Evans
31.7k54370
31.7k54370
Awesome! Thanks
– GN.
Nov 11 at 23:28
add a comment |
Awesome! Thanks
– GN.
Nov 11 at 23:28
Awesome! Thanks
– GN.
Nov 11 at 23:28
Awesome! Thanks
– GN.
Nov 11 at 23:28
add a comment |
up vote
0
down vote
For static methods, use the class name rather than super.
class Person {
constructor() {}
static isHuman() {
return 'yes I am';
}
}
class Brian extends Person {
constructor() {
super();
}
greeting() {
console.log(Person.isHuman())
}
}
const b = new Brian();
b.greeting();
add a comment |
up vote
0
down vote
For static methods, use the class name rather than super.
class Person {
constructor() {}
static isHuman() {
return 'yes I am';
}
}
class Brian extends Person {
constructor() {
super();
}
greeting() {
console.log(Person.isHuman())
}
}
const b = new Brian();
b.greeting();
add a comment |
up vote
0
down vote
up vote
0
down vote
For static methods, use the class name rather than super.
class Person {
constructor() {}
static isHuman() {
return 'yes I am';
}
}
class Brian extends Person {
constructor() {
super();
}
greeting() {
console.log(Person.isHuman())
}
}
const b = new Brian();
b.greeting();
For static methods, use the class name rather than super.
class Person {
constructor() {}
static isHuman() {
return 'yes I am';
}
}
class Brian extends Person {
constructor() {
super();
}
greeting() {
console.log(Person.isHuman())
}
}
const b = new Brian();
b.greeting();
class Person {
constructor() {}
static isHuman() {
return 'yes I am';
}
}
class Brian extends Person {
constructor() {
super();
}
greeting() {
console.log(Person.isHuman())
}
}
const b = new Brian();
b.greeting();
class Person {
constructor() {}
static isHuman() {
return 'yes I am';
}
}
class Brian extends Person {
constructor() {
super();
}
greeting() {
console.log(Person.isHuman())
}
}
const b = new Brian();
b.greeting();
answered Nov 11 at 1:01
Ryan C
678210
678210
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244904%2fcan-a-instance-method-in-a-javascript-subclass-call-its-parents-static-method%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
No, is not possible. It doesn't make sense. You need to understand how the static members work. read more about it here
– Ele
Nov 11 at 0:59