Проблемы с вызовом функций обьекта
Есть обьект.
function slider () {
this.rightScroll = "#right-scroll";
this.init = function() {
jQuery(this.rightScroll).click(function() {
this.rightStep();
});
}
this.rightStep = function() {
alert("ok");
}
}
Вызываю его.
$(document).ready(function() {
var slider1 = new slider();
slider1.init();
})
И вот что мне выдает. this.rightStep is not a function и не вызывает ее - в чем может быть проблема? |
Цитата:
|
И как обойти такое поведение - есть какое - то решение?
|
попробуйте сохранить контекст(this) в переменной
function slider () {
*!*var copy = this;*/!*
this.rightScroll = "#right-scroll";
this.init = function() {
jQuery(this.rightScroll).click(function() {
*!*copy*/!*.rightStep();
});
}
this.rightStep = function() {
alert("ok");
}
}
|
У вас там замыкание, т.е. мы можем сделать так
this.init = function() {
var $this = this;
jQuery(this.rightScroll).click(function() {
$this.rightStep();
});
}
Либо подменить this методами call/apply, но хрен знает как на ето jQuery отреагирует) |
Спасибо, попробую поиграться
|
|
i would like to appreciate you for sharing such a great info with us
:) |
| Часовой пояс GMT +3, время: 22:26. |