采用单个参数的Javascript函数。使用该参数值作为字符串,以从匹配的对象键返回适当的值。
class ViewModel {
var courses: Variable<[Course]> = Variable([])
var selectedCourses: Variable<[Course]> = Variable([])
var actions = Variable<[Action<Course, Bool>]>([])
private func generateAddActions(courses: [Course]) -> [Action<Course, Bool>] {
var actions: [Action<Course, Bool>] = []
for _ in courses {
let action = Action<Enrollment, Bool>(workFactory: { (input) -> Observable<Bool> in
let isAdded = selectedCourses.value.contains(input)
if !isAdded {
self.selectedCourses.value.append(input)
}
return Observable.just(isAdded)
})
actions.append(action)
}
return actions
}
}
}
返回的是
function someFunction(someArg) {
var message = {
bob: "Hello bob",
mike: "Hello mike",
tara: "Hello tara"
}
console.log(message + " " + message.someArg + " " + someArg + " " + message.bob);
如果在控制台日志中返回undefined,则JavaScript应返回消息“Hello bob”,因为someArg的值为“bob”,调用message.bob会返回正确的结果。
答案 0 :(得分:1)
要正确打印,您必须:
试试这个
function someFunction(someArg) {
var message = {
bob: "Hello bob",
mike: "Hello mike",
tara: "Hello tara"
}
//ES6
console.log(`${JSON.stringify(message)} ${message[someArg]} ${someArg} ${message.bob}`);
//ES5
console.log(JSON.stringify(message) + " " + message[someArg] + " " + someArg + " " + message.bob);
}
现在,在调用 someFunction('bob')时,输出为:
{"bob":"Hello bob","mike":"Hello mike","tara":"Hello tara"} Hello bob bob Hello bob
答案 1 :(得分:0)
你必须使用[]表示法,其中obj [key]与obj.key相同,但key可以是变量。
function someFunction(someArg) {
var message = {
bob: "Hello bob",
mike: "Hello mike",
tara: "Hello tara"
}
console.log(JSON.stringify(message) + " " + message[someArg] + " " + someArg + " " + message.bob);
}
someFunction("mike");
&#13;
答案 2 :(得分:0)
使用message.someArg
时,您正在&#34;告诉&#34;属性someArg或您的消息对象。
您必须使用message[someArg]
来获取动态属性。