LEARNING JAVASCRIPT - Trang 37

Note that we could save ourselves a little typing by passing the point directly to the

circle (instead of passing the x and y coordinates separately):

var

c

=

Shape

.

Circle

(

event

.

point

,

20

);

This highlights a very important aspect of JavaScript: it’s able to ascertain information

about the variables that are passed in. In the previous case, if it sees three numbers in

a row, it knows that they represent the x and y coordinates and the radius. If it sees

two arguments, it knows that the first one is a point object, and the second one is the

radius. We’ll learn more about this in Chapters

6

and

9

.

Hello, World

Let’s conclude this chapter with a manifestation of Brian Kernighan’s 1972 example.

We’ve already done all the heavy lifting: all that remains is to add the text. Before your

onMouseDown

handler, add the following:

var

c

=

Shape

.

Circle

(

200

,

200

,

80

);

c

.

fillColor

=

'black'

;

var

text

=

new

PointText

(

200

,

200

);

text

.

justification

=

'center'

;

text

.

fillColor

=

'white'

;

text

.

fontSize

=

20

;

text

.

content

=

'hello world'

;

This addition is fairly straightforward: we create another circle, which will be a back‐

drop for our text, and then we actually create the text object (

PointText

). We specify

where to draw it (the center of the screen) and some additional properties (justifica‐

tion, color, and size). Lastly, we specify the actual text contents (“hello world”).
Note that this is not the first time we emitted text with JavaScript: we did that first

with

console.log

earlier in this chapter. We certainly could have changed that text to

“hello world.” In many ways, that would be more analogous to the experience you

would have had in 1972, but the point of the example is not the text or how it’s ren‐

dered: the point is that you’re creating something autonomous, which has observable

effects.
By refreshing your browser with this code, you are participating in a venerable tradi‐

tion of “Hello, World” examples. If this is your first “Hello, World,” let me welcome

you to the club. If it is not, I hope that this example has given you some insight into

JavaScript.

Hello, World | 13

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.