Blogger Information
Blog 33
fans 0
comment 0
visits 17257
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
常用函数类型与数据类型
lucaslwk
Original
887 people have browsed it

常用函数类型与数据类型

一.常用的函数类型

1.命名函数

  1. function name(a) {
  2. return a;
  3. }
  4. console.log(name("命名函数"));

2.匿名函数

调用方式

1.赋值给变量

  1. let b = function (a) {
  2. return a;
  3. };
  4. console.log(b("匿名函数"));

2.将声明与调用二合一,立即调用函数,IIFE

  1. console.log(
  2. (function (a) {
  3. return a;
  4. })("立即调用函数")
  5. );

3.箭头函数,简化匿名函数声明

  1. let c = (a) => {
  2. return a;
  3. };
  4. console.log(c("箭头函数"));

二.常用的数据类型

1.原始类型

  1. //number
  2. console.log(typeof 123);
  3. //string
  4. console.log(typeof "123");
  5. //Boolean
  6. console.log(typeof true);
  7. //undefined
  8. console.log(typeof a);
  9. //null
  10. console.log(null == null);

2.引用类型

  1. //数组array
  2. const arr = [1, 2, 3];
  3. //对象object
  4. let obj = { id: 1, name: "王老师", age: 20 };
  5. //函数function
  6. function getname(obj) {
  7. return "name is " + obj.name;
  8. }
  9. console.log(getname(obj));
  1. //对象字面量
  2. obj2 = {
  3. id: 2,
  4. name: "张老师",
  5. age: 30,
  6. getname: function () {
  7. return "name is " + this.name;
  8. },
  9. };
  10. console.log(obj2.getname());
  11. //函数可以作为参数传递
  12. function func2(a) {
  13. console.log(a());
  14. }
  15. func2(function () {
  16. return "Hello";
  17. });
  18. //闭包:函数作为返回值
  19. function func3() {
  20. let a = 1;
  21. return function () {
  22. return a++;
  23. //注意a++与a+=1的区别,a++先返回a的值再参与运算
  24. };
  25. }
  26. const f = func3();
  27. console.log(f());
  28. console.log(f());
  29. //函数即为对象,可以添加属性和方法
  30. let func4 = () => {};
  31. func4.userName = "张三";
  32. func4.getUserName = () => {
  33. console.log(this.userName);
  34. };
  35. //注意箭头函数没有自己的this,箭头函数的this为整个windows对象
  36. func4.getUserName();
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