Blogger Information
Blog 13
fans 0
comment 0
visits 6509
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
常用函数类型和数据类型
shooter
Original
820 people have browsed it

常用函数类型

1. 命名函数

2. 匿名函数

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

运行效果图

代码

  1. <!DOCTYPE html>
  2. <html lang='en'>
  3. <head>
  4. <meta charset='UTF-8' />
  5. <meta http-equiv='X-UA-Compatible' content='IE=edge' />
  6. <meta name='viewport' content='width=device-width, initial-scale=1.0' />
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. // 1. 命名函数
  12. function getName(username) {
  13. return 'hello' + username;
  14. }
  15. console.log(getName('吴彦祖'));
  16. // 2.1 匿名函数
  17. // 第一种声明方式,将匿名函数当成值赋给一个变量
  18. let getUserName = function (username) {
  19. return 'hello' + username;
  20. };
  21. console.log(getUserName('陈冠希'));
  22. // 2.2 第二种方式将声明与调用二合一:立即调用函数,IIFE
  23. // 表达式,是用一对括号包住的
  24. console.log(
  25. (function (username) {
  26. return 'hello' + username;
  27. })('刘德华')
  28. );
  29. // 2.3 箭头函数,用来简化匿名函数的声明
  30. function sum(a, b) {
  31. console.log(a + b);
  32. }
  33. sum(1, 2);
  34. // 改成箭头函数
  35. // 将命名函数改成了匿名函数
  36. let add = function (a, b) {
  37. console.log(a + b);
  38. };
  39. add(3, 4);
  40. // 使用箭头函数来简化匿名函数
  41. // =>(胖箭头),->(瘦箭头)
  42. // 转换方法
  43. // 1. 去掉function
  44. // 2. 在参数列表与大括号之间使用'=>'
  45. acc = (a, b) => {
  46. console.log(a + b);
  47. };
  48. acc(5, 6);
  49. // 如果只有一个参数,可以不写参数列表的括号
  50. acc1 = (a) => {
  51. console.log(a + 1);
  52. };
  53. acc1(9);
  54. // 如果没有参数,括号必须加上
  55. acc2 = () => {
  56. console.log(1 + 2);
  57. };
  58. acc2();
  59. // 如果函数体只有一条语句,大括号都可以不用
  60. acc3 = () => console.log(7 + 8);
  61. acc3();
  62. </script>
  63. </body>
  64. </html>

数据类型

原始类型: number, string, boolean,undefined, null

1. 数组

2. 对象

3. 函数

效果图

代码

  1. <!DOCTYPE html>
  2. <html lang='en'>
  3. <head>
  4. <meta charset='UTF-8' />
  5. <meta http-equiv='X-UA-Compatible' content='IE=edge' />
  6. <meta name='viewport' content='width=device-width, initial-scale=1.0' />
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. // 1. 原始类型: number, string, boolean,undefined, null
  12. console.log(1 + 1);
  13. // typeof 查看数据类型
  14. console.log(typeof 1 + typeof 1);
  15. console.log('hello' + 'world');
  16. console.log(typeof ('hello' + 100));
  17. // 引用类型,array , object , function
  18. // 1. 数组
  19. const arr = [1, 'admin', [1, 2, 3], true];
  20. console.log(arr);
  21. // 访问数据元素,必须通过数组的引用(数组名称arr)来访问(arr是一个访问入口)
  22. // 数组成员 的索引是从0开始
  23. console.log(arr[1]);
  24. console.log(arr[2][1]);
  25. console.log(Array.isArray(arr));
  26. // 对象
  27. let obj = {
  28. id:1,
  29. username:'吴彦祖',
  30. num:[1,2,3],
  31. isOK: true,
  32. 'my email':'84451879@qq.com',
  33. }
  34. console.log(obj['username']);
  35. // 为了简化,并与数组区别,对象有自己的成员访问符:.
  36. console.log(obj.id);
  37. console.log(obj['my email']);
  38. function getUser(obj) {
  39. return 'id =' + obj.id + ', username =' + obj.username;
  40. }
  41. console.log(getUser(obj));
  42. // 对象是可以将数据与函数封装到一起,做为一个独立的编程单元
  43. // 对象字面量
  44. obj2 = {
  45. id:2,
  46. username:'陈冠希',
  47. isOK:true,
  48. 'my email':'84451879@qq.com',
  49. // 将一个函数转为对象的方法,封装到对象中
  50. getUser: function(){
  51. // 在对象中,使用变量this来引用对象自身
  52. return 'id =' + this.id + ',username =' +this.username;
  53. },
  54. };
  55. console.log(obj2.getUser());
  56. // 函数
  57. // 函数是对象,也是一个值,可以当成参数传递,也可以当成返回值
  58. console.log(typeof
  59. function() {});
  60. function f1(callback) {
  61. console.log(typeof callback);
  62. console.log(callback());
  63. }
  64. f1(function() {
  65. return 'hello 犀利哥';
  66. });
  67. // 函数返回值:闭包
  68. function f2() {
  69. // a是 f2的私有变量
  70. let a = 1;
  71. return function() {
  72. // return (a += 1);
  73. // 而此时,子函数中的a并不是自己的,是父函数的
  74. return a++;
  75. };
  76. }
  77. console.log(f2());
  78. const f = f2();
  79. console.log(f);
  80. console.log(f())
  81. console.log(f())
  82. console.log(f())
  83. console.log(f())
  84. console.log(f())
  85. console.log(f())
  86. // 函数就是对象,对象就可以添加属性和方法
  87. let f3 = function() {};
  88. f3.myemail = '84451879@qq.com';
  89. console.log(f3.myemail);
  90. f3.getEmail = function() {
  91. console.log(this.myemail);
  92. };
  93. f3.getEmail();
  94. </script>
  95. </body>
  96. </html>
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!