<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> //什么是函数? /* 1.什么是函数? 函数是专门用于封装代码的, 函数是一段可以随时被反复执行的代码块 2.函数格式 function 函数名称(形参列表){ 被封装的代码; } */ function toLeft() { console.log("打左转向灯"); console.log("踩刹车"); console.log("向左打方向盘"); console.log("回正方向盘"); } function toRight() { console.log("打右转向灯"); console.log("向右打方向盘"); console.log("回正方向盘"); } toLeft(); // 向右变道 toRight(); // 向左变道 toLeft(); </script> </body> </html>
优点:减少重复代码。。