![](/img/trans.png)
[英]What is the alternative equivalent of Javascript “prototype” in Typescript?
[英]What is javascript/typescript equivalent of this python function?
我无法将此语法转换为 js 或 ts:
def get_index(self):
path = self.get_request_path()
if "/_async_search" in path and "/_search":
# no index we wildcard
return next((sub_part for sub_part in path.split("/") if sub_part != ""), "*")
return None
我认为对 python 代码进行 javascript 翻译的主要困难是像在 python 代码中那样延迟加载path.split("/")
返回的列表。
为此,您可以使用生成器 function,如下所示:
function* first_substring(path) {
for(substring of path.split("/")){
if(substring !== ""){
yield substring;
}
else{
yield "*";
}
}
}
那么您的 javascript 翻译将类似于:
function get_index(){
const path = this.get_request_path();
if(path.indexOf("/_async_search") !== -1 && "/_search"){
return first_substring(path).next().value;
}
return None
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.