Blogger Information
Blog 31
fans 0
comment 0
visits 14270
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
函数类型和数据类型
木子木杉
Original
581 people have browsed it

函数类型

1.命名函数

  1. //命名:动词+名词
  2. function getName(username) {
  3. return "Hello" + username;
  4. }
  5. console.log(getName("猪老师"));

2.匿名函数
没有名字的函数
执行方式:2.1.立即执行:IIFE

  1. (function (username) {
  2. console.log("Hello" + username);
  3. })("灭绝老师");
  4. console.log(
  5. (function (username) {
  6. return "Hello" + username;
  7. })("灭绝师妹")
  8. );

IIFE:阅后即焚,不会给全局环境带来任何污染,用来创建临时作用域
node 模块,就是用IIFE来写
执行方式2.保存到变量中
2.2.函数表达式

  1. const getUserName = function (username) {
  2. return "Hello" + username;
  3. };
  4. console.log(getUserName("马老师"));

2.3.箭头函数
使用箭头函数简化匿名函数的声明
1.去掉function
2.在参数括号与大括号之间加胖箭头

  1. let f1 = function sum(a, b) {
  2. return a + b;
  3. };
  4. console.log(f1(10, 20));
  5. f1 = (a, b) => {
  6. return a + b;
  7. };
  8. console.log(f1(8, 9));
  9. //只有一个参数时()可以不写
  10. f1 = username => {
  11. return "hello" + username;
  12. };
  13. //只有一条语句,return是默认的,return可以不写
  14. f1 = x => x * 2;
  15. console.log(f1(4));
  16. //没有参数()不能省
  17. f1 = () => "jintiantiaoqi";
  18. console.log(f1());

数据类型

1.原始类型
number string boolean underfined null

  1. console.log(123, typeof 123);
  2. console.log("php", typeof "php");
  3. console.log(true, typeof true);
  4. console.log(undefined, typeof undefined);
  5. console.log(null, typeof null);

2.引用类型
array object function
2.1 array

  1. const arr = ["手机", 2, 5000];
  2. console.log(arr);
  3. console.log(arr[0]);
  4. console.log(arr[1]);
  5. console.log(arr[2]);
  6. console.log(typeof arr);
  7. console.log(Array.isArray(arr));

2.2 object

  1. console.log(123, typeof 123);
  2. console.log("php", typeof "php");
  3. console.log(true, typeof true);
  4. console.log(undefined, typeof undefined);
  5. console.log(null, typeof null);

2.3 function
2.3.1 value

  1. function f2() {
  2. let a = 1;
  3. return function () {
  4. return a++;
  5. };
  6. }
  7. console.log(f2());
  8. const f = f2();
  9. console.log(f());
  10. console.log(f());
  11. console.log(f());
  12. console.log(f());
  13. console.log(f());
  14. console.log(f());

2.3.2 object

  1. function f1(callback) {
  2. console.log(callback());
  3. }
  4. f1(function () {
  5. return "hell 朱老师";
  6. });
Correcting teacher:PHPzPHPz

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