Blogger Information
Blog 41
fans 2
comment 0
visits 29716
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
javascript基础(一)
月光下,遗忘黑暗
Original
470 people have browsed it

1.js引入方式

1.内部脚本

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>js引入方式一</title>
  6. </head>
  7. <body>
  8. <script>
  9. alert('hello world!!!');
  10. </script>
  11. </body>
  12. </html>

2.外部脚本

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>js引入方式</title>
  6. </head>
  7. <body>
  8. <script src="demo.js"></script>
  9. </body>
  10. </html>

3.内联脚本

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>js引入方式</title>
  6. </head>
  7. <body>
  8. <h1 onclick="alert('hello world!')">hello world!</h1>
  9. </body>
  10. </html>

2.变量与常量的声明与使用方式

1.变量声明

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>js引入方式</title>
  6. </head>
  7. <body>
  8. <script>
  9. let a = '123';
  10. // 切记变量不可重复定义,但可以进行更新
  11. console.log(a)
  12. a ='321';
  13. console.log(a)
  14. </script>
  15. </body>
  16. </html>

效果图

2.常量声明

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>js引入方式</title>
  6. </head>
  7. <body>
  8. <script>
  9. const A = '123';
  10. console.log(A)
  11. // 常量定义后不能更新
  12. A = '321';
  13. console.log(A)
  14. </script>
  15. </body>
  16. </html>

效果图

3.函数

1.普通函数

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <script>
  9. function getName(name) {
  10. return 'hello' + name;
  11. }
  12. console.log(getName('地球'))
  13. </script>
  14. </body>
  15. </html>

2.匿名函数

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <script>
  9. // 匿名函数就是将一个函数的声明作为值赋给一个变量或常量
  10. let sum = function (a,b) {
  11. return a + b;
  12. }
  13. console.log(sum(1,2));
  14. </script>
  15. </body>
  16. </html>

3.函数的归并参数

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>归并参数</title>
  6. </head>
  7. <body>
  8. <script>
  9. let sum = function (...arr) {
  10. console.log(arr);
  11. return arr.reduce((p, c)=>p + c);
  12. }
  13. console.log(sum(1,2,3,4))
  14. </script>
  15. </body>
  16. </html>

4.高阶函数:回调函数

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>高阶函数</title>
  6. </head>
  7. <body>
  8. <!-- 高阶函数:使用函数作为参数或者将函数作为返回的函数-->
  9. <script>
  10. function demo(f) {
  11. console.log(f)
  12. return function f1() {
  13. return 'abc';
  14. }
  15. }
  16. // 函数作为参数,这就是回调函数
  17. let a = demo(function () {
  18. });
  19. console.log(a());
  20. </script>
  21. </body>
  22. </html>

5.高阶函数:偏头函数

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>偏头函数</title>
  6. </head>
  7. <body>
  8. <script>
  9. // let sum = function (a,b,c,d) {
  10. // return a+b+c+d;
  11. // }
  12. let sum = function (a,b) {
  13. return function (c,d) {
  14. return a+b+c+d;
  15. }
  16. }
  17. let f1 = sum(1,2);
  18. // f1是一个函数
  19. console.log(typeof f1);
  20. console.log(f1(3,4));
  21. // 颗粒化
  22. sum = function (a) {
  23. return function (b) {
  24. return function (c) {
  25. return function (d) {
  26. return a+b+c+d;
  27. }
  28. }
  29. }
  30. }
  31. console.log(sum(1)(2)(3)(4));
  32. </script>
  33. </body>
  34. </html>

6.纯函数

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>纯函数</title>
  6. </head>
  7. <body>
  8. <script>
  9. let c = 100;
  10. function add(a,b) {
  11. // return a+b+c;
  12. // 去掉c就是纯函数
  13. return a+b;
  14. }
  15. console.log(add(1,2))
  16. </script>
  17. </body>
  18. </html>

7.箭头函数

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>箭头函数</title>
  6. </head>
  7. <body>
  8. <script>
  9. let sum = function (a,b) {
  10. return a+b;
  11. }
  12. // 匿名函数可以用箭头函数进行简化
  13. sum =(a,b) => {
  14. return a+b;
  15. }
  16. console.log(sum(10,20));
  17. // 如果箭头函数的代码体只有一行语句,可以删除大括号,而且自带return;
  18. sum = (a,b) => a+b;
  19. console.log(sum(3,2));
  20. // 如果只有一个参数,连参数列表的圆括号都可以删除
  21. let tips = (name) => console.log(100+name);
  22. tips(123);
  23. // 如果函数中要用到this,就不要用箭头函数了,不能当构造函数用
  24. </script>
  25. </body>
  26. </html>

8.立即执行函数

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>立即执行函数(IIFE)</title>
  6. </head>
  7. <body>
  8. <script>
  9. //声明
  10. let sum = (a,b) => console.log(a+b);
  11. sum(100,200);
  12. //调用
  13. // 立即执行函数
  14. ((a,b) => console.log(a+b))(100.200);
  15. // 在很久之前,js是不支持块作用域
  16. // if (true) {
  17. // var b = 100;
  18. // }
  19. (function () {
  20. if (true) {
  21. // 一旦将代码块用一个立即执行函数套住,那么内部声明的变量b就不会泄露到全局
  22. var b = 100;
  23. }
  24. })()
  25. console.log(b);
  26. </script>
  27. </body>
  28. </html>

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post