Blogger Information
Blog 37
fans 1
comment 0
visits 32623
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JavaScript的常用数据类型与变量常量的分类
Jason Pu?
Original
804 people have browsed it

java script简称为js,是一种基于事件驱动的异步单线程的脚本语言,通过事件循环来完成。

一:js常用数据类型

js的常用数据类型有二种:
1:原始类型:string(字符串),boolean(布尔),数值(number),undefind(不明确),null(空),symbol,值传递。
例如:

  1. let a = 100;
  2. let b = "100";
  3. let c;
  4. let d = true;
  5. let e = false;
  6. console.log(typeof(a));//number
  7. console.log(typeof(b));//string
  8. console.log(typeof(c));//undefined
  9. console.log(typeof(d),typeof(e));//boolean boolean

原始类型都是值传递的,比如:

  1. let f = 100;
  2. let g = f;
  3. console.log(g);//100
  4. f = 200;
  5. console.log(g);//100,f的更新没有影响到g

2:引用类型:对象,数组,函数
他们都是引用传递
例如:
对象:

  1. let me = {name:"Jason",age:18,work:"sales",aboutme:function(){return "姓名"+this.name+"年龄"+this.age}};
  2. console.log(me.aboutme());//姓名Jason年龄18
  3. console.log(typeof(me));//object
  4. // 对象的属性是无序集合,还可以添加属性
  5. me.tel="13888888888";
  6. console.log(me.tel);//13888888888
  7. // 数组:
  8. let mark = [99,100,98,89];
  9. // 如果用typeof(数组),也会返回object
  10. console.log(typeof(mark));//object
  11. // 可以使用新方法Array.isArray()来判断是否是数组,例如:
  12. let arr = Array.isArray(mark);
  13. console.log(arr);//true
  14. // 数组中的元素索引是从0开始,按索引来访问元素
  15. console.log(mark[0]);//99

二:js的变量和常量

** 1.传统方式:var,可以重复声明,所以后面的会覆盖前面的,不是严谨的做法

2.let变量,可以先声明不赋值,禁止重复申明,例如
**

  1. let my_name;
  2. console.log(my_name);//undefined
  3. my_name = "Jason";
  4. console.log(my_name);//Jason
  5. let my_name = "Peter";//报错了:Uncaught SyntaxError

**3.常量const:顾名思义声明后不可以删除,也不能更新,属于只读变量

4:命名规范:
注意:只能字母,数字和下线线,$,而且第一个不能是数字,标识符严格区分大小写
命名规范:**

  1. 1.蛇形:
  2. let user_age = 18;
  3. 2.驼峰式:
  4. userName = "Jasper";
  5. 3.大驼峰:
  6. UserSex = "Male";
  7. 4.匈牙利:
  8. nAge = 18;

实际工作中,尽可能首选const常量,其次才考虑let

Correcting teacher:天蓬老师天蓬老师

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