2010-06-03 5 views
1

CodeMirror 자바 스크립트 코드 편집기에서 메서드를 호출하는 중입니다. 나는 자바 스크립트에 익숙하지 않고 객체 지향적 인 것들이 어떻게 작동하는지 이해하려고 노력하고있다. 내가 믿는 것을 방법이라고 부르는 데 문제가 있습니다. 예를 들어,프로토 타입 메서드 호출 이해

var editor = CodeMirror.fromTextArea('code', options); 
editor.grabKeys(function(e) { alert("Key event");}); 

이렇게하면 Uncaught TypeError: Cannot call method 'grabKeys' of undefined이됩니다. editor 개체를 보면 grabKeys가 editor.__proto__.grabKeys에있는 것으로 보입니다.

어떻게 생각해야합니까?

http://www.javascriptkit.com/javatutors/proto.shtml : 여기

을 위해 무엇 프로토 타입 방법 전화의 좋은 설명입니다 ..

var editor = new CodeMirror.fromTextArea('code', options); 
editor.grabKeys(function(e) { alert("Key event");}); 

공지 사항 '새'연산자 :

+0

'CodeMirror.fromTextArea ('code', options)처럼 보이며''undefined'를 반환합니다. 어쩌면 그 함수에 잘못된 인수를 전달하고 있을까요? – harto

+0

내가 console.log (편집자)라면 내가 탐색 할 수있는 객체로 반환됩니다. – Tristan

답변

2

아마 yoour 코드는 다음과 같이해야한다

+0

그걸 수정하지 않은 것 같습니다. – Tristan

+1

링크 된 문서에서 다르게 제안합니다. – harto

+0

미안하지만 fromTextArea가 개체를 생성하는 유틸리티 함수라는 것을 알지 못했습니다. 그렇다면 '옵션'개체는 무엇입니까? 여기에 내용을 게시 할 수 있습니까? – Vlad