2010-06-03 19 views
1

在努力呼籲CodeMirror javascript代碼編輯器的方法。我是新來的JavaScript和試圖瞭解如何面向對象的東西的作品。我有問題叫我相信是方法。例如,理解原型方法調用

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

我應該如何來考慮這件事?

+0

貌似'CodeMirror.fromTextArea( '代碼',選項);''返回undefined'。也許你正在向這個函數傳遞錯誤的參數? – harto 2010-06-03 00:24:35

+0

如果我console.log(編輯器)它返回作爲一個對象,我可以探索。 – Tristan 2010-06-03 00:26:04

回答

2

也許yoour代碼應該是這樣的:

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

注意 '新' 運營商..

這裏是一個什麼樣的原型方法調用是一個很好的解釋:

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

+0

這似乎並沒有解決它。 – Tristan 2010-06-03 00:24:19

+1

鏈接的文檔建議不然。 – harto 2010-06-03 00:27:13

+0

對不起沒有看到fromTextArea是一個效用函數,爲你構建對象。那麼你的'選擇'對象是什麼? 你可以在這裏發佈內容? – Vlad 2010-06-03 00:35:49