[英]What is the JavaScript equivalent of this Swift code?
I learnt that you could have arrays within classes in Swift this way (quiz question example): 我了解到您可以在Swift中的类中使用数组(测验问题示例):
// Swift
class Question {
var ques:String
var ans:Bool
var expl:String
init(quesTitle:String, answer:Bool, explanation:String) {
self.ques = quesTitle
self.ans = answer
self.expl = explanation
}
}
class AllQues {
var questions:[Question] = []
init() {
questions.append(Question(quesTitle: "Is xxx true?", answer: false, explanation: "Explanation here."))
questions.append(Question(quesTitle: "This has to be true", answer: false, explanation: "But it isn't"))
}
}
Then, to output: 然后,输出:
var allQuestions = AllQues()
func loadQuestion() {
questionLabel.text = "Question \(number + 1) of \(allQuestions.questions.count)\n\n \(allQuestions.questions[number].ques)"
correctAnswer = allQuestions.questions[number].ans
explanation = allQuestions.questions[number].expl
}
I would like to know if there is an equivalent to this in JavaScript. 我想知道JavaScript中是否有与此等效的代码。
I am trying to develop a time zone converter. 我正在尝试开发时区转换器。 Here is the JavaScript code I'm trying:
这是我正在尝试的JavaScript代码:
// JavaScript
class City {
constructor(city, country, offset) {
this.city = city;
this.country = country;
this.offset = offset;
}
}
class Cities {
// ???
}
I am try to give Cities
an array containing several of City
. 我尝试给
Cities
一个包含几个City
的数组。 As you can see, Swift has an easy way to do it. 如您所见,Swift有一个简单的方法。 Is there something similar I can do in JavaScript?
我可以用JavaScript做类似的事情吗?
class Cities {
constructor() {
this.cities = [];
this.cities.push(new City("foo", "bar", "baz"));
this.cities.push(new City("snap", "crackle", "pop"));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.