壓痕定義你的CoffeeScript代碼結構,所以如果你的縮進是關閉的,然後你的代碼也關閉。
在第一種情況:
$(document).on "turbolinks:load", ->
$('#redemption_amount').keyup updateWorth
$('#redemption_amount').change updateWorth
updateWorth = ->
discount = ($('#discount').val()/100)
worth = $(this).val() * discount
$('#redemption_worth').html(worth.toFixed(2))
JavaScript的樣子:
var updateWorth;
$(document).on("turbolinks:load", function() {
$('#redemption_amount').keyup(updateWorth);
$('#redemption_amount').change(updateWorth);
});
updateWorth = function() { ... };
因此受到時間keyup
和change
被稱爲,updateWorth
的值是一個函數,一切工作正常。
在第二種情況:
$(document).on "turbolinks:load", ->
$('#purchase_amount').keyup updatePoints
$('#purchase_amount').change updatePoints
updatePoints = ->
points = (Math.round($(this).val() * 0.92))
$('#purchase_points').html(points)
JavaScript的結束,如:
$(document).on("turbolinks:load", function() {
var updatePoints;
$('#purchase_amount').keyup(updatePoints);
$('#purchase_amount').change(updatePoints);
updatePoints = function() { ... };
});
你壓痕提出updatePoints
內回調。當keyup
和change
被稱爲,updatePoints
的價值是undefined
因爲updatePoints = function() { ... }
部分還沒有達到呢。拇指
規則:
- 定義的東西(在代碼的行順序)使用它們之前。
- 要非常小心,並與您的空白是一致的:如果你縮進兩個空格在一個地方,然後縮進兩個空格到處。
你應該說:
updateWorth = ->
discount = $('#discount').val()/100
worth = $(@).val() * discount
$('#redemption_worth').html(worth.toFixed(2))
$(document).on "turbolinks:load", ->
$('#redemption_amount').keyup updateWorth
$('#redemption_amount').change updateWorth
和:
updatePoints = ->
points = (Math.round($(@).val() * 0.92))
$('#purchase_points').html(points)
$(document).on "turbolinks:load", ->
$('#purchase_amount').keyup updatePoints
$('#purchase_amount').change updatePoints