LEARNING JAVASCRIPT - Trang 212

My goal in this chapter is less to explain the syntax of JavaScript than to get you to

think about why. Why do you need functions? Thinking about a function as a sub‐

routine offers one answer for that question: to avoid repeating yourself. Subroutines

give you a way to package commonly used functionality—a pretty obvious benefit.

Avoiding repetition by packaging code is such a foundational con‐

cept that it’s spawned its own acronym: DRY (don’t repeat your‐

self). While it may be linguistically questionable, you’ll find people

using this acronym as an adjective to describe code. “This code

here could be more DRY.” If someone tells you that, they are telling

you that you are unnecessarily repeating functionality.

Pure functions are a slightly harder sell—and answer the question of “why” in a more

abstract fashion. One answer might be “because they make programming more like

math!”—an answer that might reasonably prompt a further question: “Why is that a

good thing?” A better answer might be “because pure functions make your code eas‐

ier to test, easier to understand, and more portable.”
Functions that return a different value under different circumstances, or have side

effects, are tied to their context. If you have a really useful function that has side

effects, for example, and you pluck it out of one program and put it into another, it

may not work. Or, worse, it may work 99% of the time, but 1% of the time causes a

terrible bug. Any programmer knows that intermittent bugs are the worst kind of

bugs: they can go unnoticed for long periods of time, and by the time they’re discov‐

ered, locating the offending code is like finding a needle in a haystack.
If you are wondering if I’m suggesting that pure functions are better, I am: you should

always prefer pure functions. I say “prefer” because sometimes it’s just plain easier to

make a function with side effects. If you’re a beginning programmer, you’ll be

tempted to do it quite often. I’m not going to tell you not to, but I am going to chal‐

lenge you to stop and think if you can find a way to use a pure function instead. Over

time, you’ll find yourself naturally gravitating toward pure functions.
Object-oriented programming, which we covered in

Chapter 9

, offers a paradigm

that allows you to use side effects in a controlled and sensible manner, by tightly

restricting their scope.

Functions Are Objects

In JavaScript, functions are instances of the

Function

object. From a practical per‐

spective, this should have no bearing on how you use them; it’s just a tidbit to file

away. It is worth mentioning that if you’re trying to identify the type of a variable

v

,

typeof v

will return

"function"

for functions. This is good and sensible, in contrast

to what happens if

v

is an array: it will return

"object"

. The upshot is that you can

188 | Chapter 13: Functions and the Power of Abstract Thinking

Liên Kết Chia Sẽ

** Đây là liên kết chia sẻ bới cộng đồng người dùng, chúng tôi không chịu trách nhiệm gì về nội dung của các thông tin này. Nếu có liên kết nào không phù hợp xin hãy báo cho admin.