Difference between $ (document) .on and ($ document) .on in CoffeeScript?

My friend uses ($ document).onCoffeeScript in his code. Is this $(document).ondifferent from the usual one , and if so, how?

+4
source share
1 answer

In CoffeeScript, calling a function with arguments does not require parentheses.

For instance:

console.log("Hello") // Hello
console.log "Hello"  // Hello

So keep in mind that they are equivalent:

$document = $(document)
$document = $ document
$document = ($ document)

However, brackets under certain circumstances are necessary to eliminate the ambiguity of meaning.

For example, you want the function to be oncalled when the function returns $():

$(document).on() // on function called on the return of $() function

But this will not work as intended:

$ document.on() // $() function called with document.on() return!

, , on $(), :

($ document).on() // on function called on the return of $() function

, CoffeeScript,

.

:)

+5

Source: https://habr.com/ru/post/1570331/


All Articles