2014-07-05 3 views
0

나는 자바 스크립트를 배우고 Knockout.JS의 예에서이 코드를 건너 왔어요 오전 : 나는이 권리를 이해한다면학습 자바 스크립트 : 쉼표로 멤버 배열 선언?

var viewModel = { 
    people: [ 
     new Person("Annabelle", ["Arnie", "Anders", "Apple"]), 
     new Person("Bertie", ["Boutros-Boutros", "Brianna", "Barbie", "Bee-bop"]), 
     new Person("Charles", ["Cayenne", "Cleopatra"]) 
     ], 
    showRenderTimes: ko.observable(false) 
}; 

는, '사람' '사람'객체의 배열을 포함하는 멤버 변수입니다. 그러나 배열 뒤에 콤마가 오는 이유는 무엇입니까? 'showRenderTimes'가 클래스에서 선언 된 다음 멤버 변수가 아닌가? 아니면 '국민'회원의 일부 확장인가? 그 콤마가 세미콜론이 아닌 이유는 무엇입니까?

여기 당신이 그것을 필요로하는 경우의 전체 목록입니다 : peopleshowRenderTimes : 여기

var Person = function(name, children) { 
    this.name = name; 
    this.children = ko.observableArray(children); 

    this.addChild = function() { 
     this.children.push("New child"); 
    }.bind(this); 
} 

// The view model is an abstract description of the state of the UI, but without any knowledge of the UI technology (HTML) 
var viewModel = { 
    people: [ 
     new Person("Annabelle", ["Arnie", "Anders", "Apple"]), 
     new Person("Bertie", ["Boutros-Boutros", "Brianna", "Barbie", "Bee-bop"]), 
     new Person("Charles", ["Cayenne", "Cleopatra"]) 
     ], 
    showRenderTimes: ko.observable(false) 
}; 

$(document).ready(function() { 
    // Define a "Person" class that tracks its own name and children, and has a method to add a new child 
    ko.applyBindings(viewModel); 
}); 

답변

3

우리는 두 개의 필드가있는 viewModelObject 있습니다.

var viewModel = {people:'something', showRenderTimes:'somethingElse'}; 

당신은 혼수 상태 , 각 필드를 구분해야하고 거기 그 이유는 다음과 같습니다

그래서 단순화는 다음과 같습니다.

또 다른 예 : 여기

var viewModel = { 
    people: [], 
    showRenderTimes: 'something' 
}; 
0

는 자바 스크립트 객체를 생성하는 방법은 다음과 같습니다

var obj = { 

    name1:value1, 
    name2:value2, 
    name3:value3 

};