admin管理员组文章数量:1323723
I am trying to find an easy way to call parent function from child one. However, couldn't really found a way to directly call it from child instance as like in other programming languages.
class ExchangeHandler {
constructor(getMarketsUrl, getMarketDataBaseUrl, interval) {
this.interval = interval;
this.getMarketsUrl = getMarketDataBaseUrl;
this.getMarketDataBaseUrl = getMarketDataBaseUrl;
}
getMarketsUrl() {
return this.getMarketsUrl;
}
getMarketDataBaseUrl() {
return this.getMarketDataBaseUrl;
}
buildQueryUrl(params) {
return querystring.stringify(params);
}
getIntervalParam() {
return config.intervalToExchangeInput[[config.exchange]][[this.interval]];
}
}
class BittrexHandler extends ExchangeHandler {
constructor(interval) {
super(".1/public/getmarkets",
".0/pub/market/GetTicks",
interval);
}
buildGetMarketTickerUrl(symbol) {
return super.getMarketDataBaseUrl() + "?"
+ super.buildQueryUrl({marketName: this.buildSymbolParam(symbol),
tickInterval: super.getIntervalParam()})
}
buildSymbolParam(symbol) {
return "BTC-" + symbol;
}
}
var bittrexHandler = ExchangeHandlerFactory.getExchangeHandler("bittrex", "hour");
console.log("getMarketsUrl: " + bittrexHandler.getMarketsUrl());
And I get
TypeError: bittrexHandler.getMarketsUrl is not a function
Isn't this possible in javascript? Thanks a lot!
I am trying to find an easy way to call parent function from child one. However, couldn't really found a way to directly call it from child instance as like in other programming languages.
class ExchangeHandler {
constructor(getMarketsUrl, getMarketDataBaseUrl, interval) {
this.interval = interval;
this.getMarketsUrl = getMarketDataBaseUrl;
this.getMarketDataBaseUrl = getMarketDataBaseUrl;
}
getMarketsUrl() {
return this.getMarketsUrl;
}
getMarketDataBaseUrl() {
return this.getMarketDataBaseUrl;
}
buildQueryUrl(params) {
return querystring.stringify(params);
}
getIntervalParam() {
return config.intervalToExchangeInput[[config.exchange]][[this.interval]];
}
}
class BittrexHandler extends ExchangeHandler {
constructor(interval) {
super("https://bittrex./api/v1.1/public/getmarkets",
"https://bittrex./Api/v2.0/pub/market/GetTicks",
interval);
}
buildGetMarketTickerUrl(symbol) {
return super.getMarketDataBaseUrl() + "?"
+ super.buildQueryUrl({marketName: this.buildSymbolParam(symbol),
tickInterval: super.getIntervalParam()})
}
buildSymbolParam(symbol) {
return "BTC-" + symbol;
}
}
var bittrexHandler = ExchangeHandlerFactory.getExchangeHandler("bittrex", "hour");
console.log("getMarketsUrl: " + bittrexHandler.getMarketsUrl());
And I get
TypeError: bittrexHandler.getMarketsUrl is not a function
Isn't this possible in javascript? Thanks a lot!
Share Improve this question asked Apr 15, 2018 at 23:33 quartaelaquartaela 2,76720 gold badges70 silver badges109 bronze badges 3-
You’re using
super.methodName
. Do you understand what the prototype is? – evolutionxbox Commented Apr 15, 2018 at 23:42 - @evolutionxbox I am trying to learn JS can you elaborate your question? – quartaela Commented Apr 16, 2018 at 0:04
- 1 I would remend you learn about JS prototypes – evolutionxbox Commented Apr 16, 2018 at 7:29
1 Answer
Reset to default 7You can't have a data property and a method with the same name as you do with getMarketsUrl
. They occupy the same property slot on the object. Change the name of one of them.
When you're trying to execute the method, the interpreter is finding the data property first and thus you can't call the method in the normal way.
In addition, you should not be using super
to just call a non-overriden method on the object. For example, change this:
buildGetMarketTickerUrl(symbol) {
return super.getMarketDataBaseUrl() + "?"
+ super.buildQueryUrl({marketName: this.buildSymbolParam(symbol),
tickInterval: super.getIntervalParam()})
}
to this:
buildGetMarketTickerUrl(symbol) {
return this.getMarketDataBaseUrl() + "?"
+ this.buildQueryUrl({marketName: this.buildSymbolParam(symbol),
tickInterval: this.getIntervalParam()})
}
本文标签: javascriptES6 call parent function from inherited classStack Overflow
版权声明:本文标题:javascript - ES6 call parent function from inherited class - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742118574a2421586.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论