2013-08-12 2 views
1

Backbone.View를 확장하는 기본 클래스가 있습니다. 하위 클래스에서 '초기화'를 재정의 한 후 super의 '초기화'를 호출 할 수 있기를 원합니다. 가장 강력하고 명확한 방법으로 자바 스크립트에서 확장 클래스의 수퍼를 호출 할 수 있습니까?Javascript 백본 클래스의 슈퍼 메서드 호출

이 (Super in Backbone)을 보았습니다. 수퍼 클래스가 누구인지 알 필요없이이를 수행하는 명확한 방법이 있습니까?

App.Views.BaseView = Backbone.View.extend({ 
    initialize: function(templateContext){ 
     this.super_called = true; 
    } 
}); 

모든 하위보기에 대해 이미 작성된 초기화 기능을 활용하고 싶습니다.

App.Views.ChildViewWorks = App.Views.extend({}); 
var newView = new App.Views.ChildViewWorks(); 
alert(newView.super_called); // print true 

App.Views.ChildViewDoesNotWork = App.Views.extend({ 
    initialize: function(templateContext){ 
     this.super_called = false; 
     //what line of code can I add here to call the super initialize()? 
    } 
}); 
var newViewWrong = new App.Views.ChildViewDoesNotWork(); 
alert(newViewWrong.super_called); //now equal to false because I have not called the super. 
+0

당신은 App.Views가 정의 된 방법을 제공 할 수 있습니까? – jcbelanger

답변

2
App.Views.ChildViewDoesNotWork = App.Views.BaseView.extend({ 
    initialize: function(templateContext){ 
     this.super_called = false; 
     App.Views.BaseView.prototype.initialize.call(this); 
    } 
}); 
+0

최종 답변은 슈퍼를 명시 적으로 알지 못하고 슈퍼를 호출하는 교활한 방법이 없다는 것입니다. –

+0

상속 선언문 컨텍스트에서 상속 클래스의 속성에 저장할 수 있습니다. – Creynders

+0

https://github.com/creynders/demiurge/blob/master/src/demiurge.js#L35 참조 – Creynders

관련 문제