선택자가없는 jQuery 플러그인에서 작업하고 있습니다. 초기화 할 때 함수가있는 객체를 인스턴스화합니다. 이 함수에서는 클로저를 사용해야합니다. 이 클로저에서는 초기 객체 함수를 호출하고 싶습니다.클로저에서 javascript 클래스 함수를 호출하는 방법
더 명확히하기 위해 다음은 단순화 된 코드 버전입니다.
HTML
<script src="/path/to/jquery/2.1.1/jquery.min.js"></script>
<script src="/path/to/my/script/myeditor.js"></script>
<div class="editable">Div1</div>
<div class="editable">Div2</div>
<script>
$.myeditor({
option1: 'a',
option2: 'b'
});
</script>
myeditor.js
function ($) {
var MyEditor = function (options)
{
this.$options = $.extend(true, {}, $.myeditor.defaults, options);
this.init();
};
$.myeditor = function (options = {})
{
return new MyEditor(options);
};
$.flyeditor.defaults = {
option1: '1',
option2: '2'
};
MyEditor.prototype.notify = function(message = '')
{
console.log(message);
};
MyEditor.prototype.init = function()
{
// Do stuff
$('.editables').each(function() {
$this = $(this);
// Here comes the error
notify($this.html());
});
};
}(jQuery);
문제는 notify(this.html());
가 ReferenceError: notify is not defined
가 어떻게이 방법을 통지 도달 할 수있는 오류를 발생시킵니다이다?
'MyEditor.prototype.notify = 기능 (메시지 = '') {'유효한 자바 스크립트, 당신은 전처리를 사용하지 않습니까? –