Вход

Просмотр полной версии : Проблемы с вызовом функций обьекта


meryfelow
22.07.2011, 00:03
Есть обьект.
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
и не вызывает ее - в чем может быть проблема?

kobezzza
22.07.2011, 00:06
this.rightStep
this в данном контексте является объектом jQuery, т.к. вы его используете внутри метода click.

meryfelow
22.07.2011, 00:08
И как обойти такое поведение - есть какое - то решение?

melky
22.07.2011, 00:10
попробуйте сохранить контекст(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");
}
}

kobezzza
22.07.2011, 00:10
У вас там замыкание, т.е. мы можем сделать так
this.init = function() {
var $this = this;
jQuery(this.rightScroll).click(function() {
$this.rightStep();
});

}

Либо подменить this методами call/apply, но хрен знает как на ето jQuery отреагирует)

meryfelow
22.07.2011, 00:12
Спасибо, попробую поиграться

adela
29.05.2013, 09:38
http://www.test-inside.com/
http://www.unifesp.br
https://www.facebook.com/Cisco
http://www.prmia.org
http://www.test-inside.com/220-801.htm

adela
29.05.2013, 09:39
i would like to appreciate you for sharing such a great info with us
:)