Blogger Information
Blog 34
fans 0
comment 0
visits 24448
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
javascript-基础(一)
CY明月归
Original
352 people have browsed it

1. 实例演示常用函数类型

  1. //作用域: 块作用域,函数作用域, 代码成员的可见范围
  2. //常量、命名规则写法特点:必须是: 字母,数字, 下划线"_", "$"(四种),其它字符均是非法符号;首字母不得使用"数字"
  3. // 1. 命名函数
  4. function sayHello(name){
  5. return 'Hello:'+name;
  6. }
  7. //console.log(sayHello('佐罗'))
  8. // 2. 匿名函数
  9. let say = function(name){
  10. return 'Hello:'+name;
  11. }
  12. //console.log(say('zy1'))
  13. console.log(
  14. function(name){
  15. return 'Hello:'+name;
  16. }('zy2')
  17. )
  18. //3、箭头函数
  19. // 转化方法
  20. // 1. 去掉 function
  21. // 2. 在参数列表与大括号之间使用 '=>'
  22. //如果函数调用出现 undefined 是因为函数没有return
  23. res =()=> console.log('hello,there!')
  24. res()
  25. res = name => { return 'Hello:'+name };
  26. console.log(res('zy3'))
  27. res = (content,name) => {return content + '< -- >' + name;}
  28. console.log(res('Hello','zy4'))

2.实例演示常用数据类型

  1. // 1. 原始类型: number, string, boolean,undefined, null 一个变量对应一个值,标量
  2. //console.log(100+200)
  3. console.log(typeof 100)
  4. console.log(typeof -100)
  5. console.log(typeof '100')
  6. console.log(typeof 'hello')
  7. console.log(typeof true)
  8. console.log(typeof undefined)
  9. console.log(typeof a)
  10. console.log(null)
  11. let b= 1;console.log(typeof b)
  12. console.log(typeof(1+true))//因为不同的类型的数据,不能直接运算,直接运算会发生类型自动转换,
  13. //先转换,再运算
  14. console.log(1+true)
  15. console.log('------')
  16. // 2. 引用类型, array, object, function
  17. //值类型的就用typeof判断,引用类型的就用instanceof判断
  18. let arr1 = [1,'name',2,'password',3,'email']
  19. arr1 instanceof Object//true
  20. //typeof 返回 object
  21. console.log(typeof {name:'zy',address:'xxx号xxx1212室'})
  22. // 一个变量保存的是一个集合,并非单值,访问时必须通过这个变量的引用来访问
  23. let arr1 = [1,'name',2,'password',3,'email']
  24. //----------0---1----2---3--------4----5
  25. console.log(arr1[0])//1 下标从0开始
  26. console.log(arr1[5])//emial 下标从0开始
  27. let arr2 = [1,'name',2,'password',3,'email',4,['aaaa','bbbbb','ccccc']]
  28. console.log(arr2[7][1])//bbbbb
  29. //引用类型判断不能用typeof
  30. console.log(Array.isArray(arr1))

!!对象和函数的关系!!

对象可以不是函数,但函数是对象!

  1. //对象=>关联数组
  2. let obj1={
  3. 'name':'zy',
  4. 'password':'121212',
  5. 'email':'123@qq.com',
  6. 'address':'xxx号xxx1212室',
  7. '地址':'xxx号xxx1213室',
  8. '位 置':'xxx号xxx1214室'
  9. }
  10. console.log(obj1.address)
  11. console.log(obj1.地址)
  12. console.log(obj1['位 置'])
  13. function getInfo(obj){
  14. return 'name:'+obj.name+' address:'+obj.address;
  15. }
  16. console.log(getInfo(obj1))
  17. let obj2={
  18. 'name':'佐罗',
  19. 'password':'131313',
  20. 'email':'321@qq.com',
  21. 'address':'xxx号xxx3333室',
  22. getInfo: obj =>{
  23. return 'name:'+obj.name+' address:'+obj.address;
  24. }
  25. }
  26. console.log(obj2.getInfo(obj2))
  27. console.log('====')
  28. // 函数是对象,也是一个值,可以当成参数传递,也可以当成返回值
  29. function f(printInfo) {
  30. console.log(typeof(printInfo))
  31. return printInfo()
  32. }
  33. console.log(f(function(){
  34. return 'name:'+obj2.name+' emial:'+obj2.email;
  35. }))
  36. console.log(f(()=>{
  37. return 'name:'+obj2.name+' emial:'+obj2.email;
  38. }))
  39. //闭包-函数当成返回值
  40. function f2() {
  41. // a是 f2的私有变量
  42. let a = 1;
  43. return function() {
  44. return a++;
  45. };
  46. }
  47. let fr = f2();//存储后后面才能累加
  48. console.log(fr())
  49. console.log(fr())
  50. console.log(fr())
  51. console.log('--------')
  52. //函数就是对象,对象可以添加属性和方法
  53. f3 = function(){}
  54. f3.email = 'newemail'
  55. f3.age=18
  56. //console.log(f3.email)
  57. //console.log(f3.name)
  58. f3.getInfo = function(){
  59. console.log(f3.age);
  60. console.log(this.email);
  61. console.log(this.name);
  62. }
  63. f3.getInfo()
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