tailieunhanh - Học JavaScript qua ví dụ part 16

Functions Một máy tính bỏ túi thực hiện chức năng nhất định. Bạn nhấn nút, gửi thông tin đến máy tính, nó thực hiện một phép tính, và gửi lại kết quả. Bạn không quan tâm đến những gì bên trong máy tính transpires, bạn chỉ muốn kết quả. Đó là những gì một chức năng nào. Các chức năng được độc lập, đơn vị của một chương trình được thiết kế để thực hiện một nhiệm vụ nhất định như tính toán các khoản thanh toán thế chấp, hiển thị hình ảnh ngẫu nhiên, hoặc kiểm tra đầu vào hợp. | chapter 7 Functions What Is a Function A pocket calculator performs certain functions. You push the buttons send information to the calculator it performs a calculation and sends back the results. You don t care about what transpires inside the calculator you just want the results. That s what a function does. Functions are self-contained units of a program designed to accomplish a specified task such as calculating mortgage payments displaying random images or checking for valid input. They can be used over and over again and thus save you from repetitious programming. They are also used to break up a program into smaller modules to keep it better organized and easier to maintain. JavaScript has a large number of its own built-in functions and now you can create your own. By definition a function is a block of statements that not only performs some task but also returns a value. A function is an independent part of your program and not executed until called. A function is often referred to as a black box. It s like the pocket calculator Information goes into the black box or calculator as input and the action or value returned from the box is its output. What goes on inside the box is transparent to the user. The programmer who writes the function is the only one who cares about those details. When you use you send something like a string of text to the function and it sends some text back to the browser. You don t care how it does its job you just expect it to work. If you send bad input you get back bad output or maybe nothing hence the expression Garbage in garbage out. Functions are like miniscripts. They contain JavaScript statements that behave as a single command and can be called repeatedly throughout a program without rewriting the code. The terms function and method are often used interchangeably. A method refers to a function that is used with JavaScript objects covered in Chapter 8 Objects . A function as used in this chapter is a .

TỪ KHÓA LIÊN QUAN