我有一個CoffeeScript對象,在觸發操作後給我一個奇怪的錯誤。Coffeescript對象方法不是函數
的對象加載無事,但一旦動作完成後觸發回調我收到錯誤:
this.update不是一個函數 返回this.update(值);
有沒有人有一個想法,爲什麼發生這個錯誤?我最好的猜測是這個這個對象裏面的jQuery.rating調用實際上是指一個jQuery對象,而不是評級對象?
我CoffeeScript的代碼是:
jQuery ->
new Rating()
class Rating
constructor: ->
$('.auto-submit-star').rating
callback:
(value, link) -> @update value
update: (value) =>
$.ajax
type: 'post'
url: $('#new_rating').attr('action')
data: 'rating': value
.done (msg) ->
alert(msg)
代碼編譯爲:
var Rating,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
Rating = (function() {
function Rating() {
this.update = __bind(this.update, this);
$('.auto-submit-star').rating({
callback: function(value, link) {
return this.update(value);
}
});
}
Rating.prototype.update = function(value) {
return $.ajax({
type: 'post',
url: $('#new_rating').attr('action'),
data: {
'rating': value
}
}).done(function(msg) {
return alert(msg);
});
};
return Rating;
})();
感謝您的信息,你是正確的脂肪箭頭解決了這個問題。欣賞信息。 – Asciant 2013-02-19 07:24:02