CoffeeScript and Named Functions

Elsewhere, an argument has arisen over the terminology of a named function in CoffeeScript. In particular someone referred to something like this:

 foo = ->
    console.log("bar")

as a named function. But its been objected that everything in CoffeeScript is anonymous functions and there are no named functions. This is certainly true, CoffeeScript only has function expressions which can then be stored in a variable. But I don’t think that means it is wrong to call this a named function.

As I see it, it is a named function because its a function that has been given a name. True, its not a named function in the same way that some other languages have named functions, but I think its close enough that it’s not inappropriate to call it a named function. To insist otherwise just seems to be nitpicking.

Am I out to lunch in thinking that insisting that this isn’t a named function is just nitpicking?

7

CoffeeScript is inexorably tied to JavaScript, and JavaScript differentiates between the following expressions:

function foo() { ... }
var foo = function () { ... }

In fact, you can even write:

var foo = function bar () { ... }

Since this difference matters in JavaScript, it makes sense to use the same terms when talking about CoffeeScript. However, CoffeeScript does not support anything like the function foo () syntax, so we can say it does not have “named” functions.

In a sense, the name is part of the function definition in the function foo() { ... }, where in the other case you just create a function and assign it to a variable. This is reflected, for example, in the (nonstandard) name property of functions: in the first case, foo.name will give you "foo" and in the second it will give you "".

Additionally, in JavaScript, these also differ in terms of how they get introduced to the scope: the first version is “hoisted” and available throughout its scope where the second definition is only available after it’s assigned.

Basically, just think of it as JavaScript-specific jargon, which is transferred to CoffeeScript because CoffeeScript is so closely related to JS.

7

It’s worth noting that the user explicitly stated they were turning an “anonymous function into a named function”, both terms having a strong, existing meanings and notably different functionalities in in the JavaScript world. Given the existing meaning, the were doing no such thing, and I pointed this out.

CoffeeScript is not so far removed from JavaScript that you should be redefining terms they both share to mean something else in one language. CoffeeScript doesn’t exist in some bubble, removed from its JavaScript implementation the way you might argue that C++ is separated from Assembly. Knowing the difference between an anonymous function and a named function matters, because if you expect your “named” CoffeeScript function to behave like an actual named function, you’re going to be disappointed:

doStuff() # I cause an error

# ... later

doStuff = (x,y) ->
  alert("Were I actually a named function, this would work!")

The equivalent JavaScript would work fine, with a real named function:

doStuff(); // I work just fine!

// later....

function doStuff() {
  alert("I'm a real named function!")
}

You might be right that I’m just “nitpicking”, but so what? The subtle points in computer programming matter, and being “technically” correct is important. It’s the difference between writing code that happens to work, and actually understanding why your code is correct.

4

Am I out to lunch in thinking that insisting that this isn’t a named function is just nitpicking?

No. After all, in terms of semantics, your function reference is stored in a variable, which you can refer to via a variable name.

Definitely not a nitpick, imo. I use it extensively for readability:

readfile()
dothis()
dothat()
thistoo()
writefile()

function readfile() {
    ...
}
...

Thus:

Real named function in “coffeescript”

hello()

`function hello() {`
console.log 'hello'
dothings()
`}`

You escape pure JS via the backtick `

Note that you can’t indent on your function body.

Cheers

Don’t waste time arguing with pedants. It’s never fruitful. Yes, everyone knows what you mean by a “named function” in CoffeeScript, even if it’s technically incorrect. No, using technically incorrect but widely understood terminology has no bearing on the correctness or incorrectness of the proposed solution. Is it possible to be more precise without gaining some awkwardness of phrasing? Probably not. However, that doesn’t mean people will let it slide. Just imagine this guy on the other end of the conversation.

The reason it’s technically incorrect is because you haven’t named the function, you’ve named a reference to the function. Consider:

foo = bar = ->
  console.log "What's my name?"

What’s the function’s name? foo and bar are both referencing the same function, but have different names. Also, either foo or bar could be reassigned at any time to reference a different function, or even a different type altogether, without changing the function itself.

So, the way I read this is like this:

When you declare a function in a JavaScript console using

var foo = function() { ... }

and then invoke foo without parentheses, it returns

function() { ... }

However, if you define it using

function foo() { ... }

and then invoke foo again without parentheses, it returns

function foo() { ... }

In the first case, I’d say you’re declaring a variable named “foo” storing an anonymous function in it. In the second case, I’d say you’re actually declaring a named function called “foo”.

Because CoffeeScript uses the first pattern, I’d agree that it’s technically correct that CoffeeScript’s functions are all anonymous functions being stored in named variables.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật