Blogger Information
Blog 47
fans 1
comment 0
visits 40470
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js的常用函数类型与常用数据类型
新手1314
Original
533 people have browsed it

常用函数类型

1.命名函数

代码:

  1. <script>
  2. function getPwd(password){
  3. return '我的密码是:'+ password;
  4. }
  5. console.log(getPwd(123456));
  6. </script>

实现:

2.匿名函数

代码:

  1. 两种声明方式:(两种方式都已放在script里)
  2. 1.将匿名函数当成值赋给一个变量
  3. let getPwd = function (password) {
  4. return "我的密码是:" + password;
  5. };
  6. console.log(getPwd(123456)); 打印语句
  7. 2.声名与调用二合一
  8. console.log(
  9. (function (password) {
  10. return "我的密码是: " + password;
  11. })(123456)
  12. );

实现:

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

代码:

  1. 最简化代码:
  2. let getPassWord = password => "我的密码是: " + password;
  3. console.log(getPassWord(123456));

实现:

常用数据类型

1.原始类型:number,srting,boolean,undefined,null

代码:

  1. numbei:console.log(typeof 123456); 输出为 number
  2. srting:console.log(typeof "我的密码是"); 输出为 string
  3. boolean:console.log(typeof true); 输出为 boolean
  4. undefined: let a; console.log(typeof a); 输出为 undefined
  5. null:console.log(null); 输出为 :null

2.引用类型:array(数组),object(对象),function(函数)

代码:

  1. (引用类型判断不能用typof)
  2. (数组的索引要从0开始,例如要输出打印1的代码为console.log(arr[0]);)
  3. 1.array: const arr = [1,2,3,4]; console.log(arr);
  4. 2.object: let object ={id:1,user:'新手1314',phone:123456,}
  5. 最简单的打印方法:console.log(boject.user) 打印结果:新手1314
  6. 函数调用方法:function getUser(object) {
  7. return "我的名字是:" + object.user;
  8. }
  9. console.log(getUser(object));
  10. 输出打印结果为:我的名字是:新手1314
  11. 3.function:
  12. (函数是对象,也是一个值,可以当成参数传递,也可以当成返回值)
  13. console.log(typeof function(){});
  14. 输出打印结果为:function(把函数当对象)
  15. funtion get(user){
  16. console.log(user());
  17. }
  18. get(function(){
  19. return '新手1314';
  20. });
  21. 输出打印的结果为:新手1314.(把函数当参数传递)
  22. function f2(){
  23. let a = 1;
  24. return function (){
  25. a++;
  26. }
  27. }
  28. const f = f2(); (把function(){a++;}赋值给f f变为函数,后面调用f函数)
  29. console.log(f()); 输出结果为:1
  30. console.log(f()); 输出结果为:2
  31. (把函数当成返回值,闭包)
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