Blogger Information
Blog 23
fans 0
comment 3
visits 23407
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js常用数据类型,变量与常量的声明与赋值
a.
Original
1031 people have browsed it

1.常量变量的声明和赋值

传统的JS没有常量,且变量可以重复声明

1.1 变量的声明

let name;
声明后并没有赋值,他的默认值为undefined

1.2 变量的赋值(初始化,第一次赋值)

name = "PHP中文网";

我们可以声明时直接赋值

let name = "php中文网";

1.3 常量

  • 常量是特殊的变量:只读变量
  • 常量声明后既不能删除也不能更新,所以声明的时候必须初始化赋值

const APP = "PHP中文网";

常量 ,变量,函数名。。。都叫JS标识符
1.只能用字母,数字,下划线,$,且第一个不能是数字
2.标识符是严格区分大小写的
3.命名规范:
1.蛇形 let user_name = "php中文网";
2.驼峰形 let userName = "php中文网";
3.大驼峰 let UserName = "php中文网";
4.匈牙利式 let oBody = "php中文网";

1.4 总结

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

2.JS中常用数据类型

2.1 原始类型

是构成其它类型的基础

2.1.1 数值类型(number):整数和小数

let age = 18;

2.1.2 字符串类型(string)

let email = "php@php.cn";

2.1.3 布尔类型

let ismarried = true ;

2.1.4 undeifned类型

未初始化变量的默认值

  1. let gender;
  2. console.log(gender);//此时会显示undeifned
  3. console.log(gender,typeof gender);
  4. //直接显示undeifned类型

2.1.5 null,空对象

let obj = null;
console.log(obj,typeof null); //返回的是object

2.1.5 符号,创建对象属性的唯一标识

  1. let s = Symbol('custom symbol');
  2. console.log(s,typeof s); //显示的就是symbol类型

使用typeof 检测数据类型

console.log(age,typeof age);

字符串拼接使用”+“号

console.log('邮箱' + email);

原始类型都是值传递

  1. let a = 100 ;
  2. //将变量的值,传递到了b中
  3. let b = a;
  4. a = 200;
  5. console.log(a);
  6. console.log(b);
  7. //a的更新不会影响到B的值

2.2 引用类型

对象(object),数组(array),函数(function)他们都是引用传递

2.2.1 对象字面量

  1. let user = {
  2. id:1,
  3. name:'誓言',
  4. 'my email':'php@php.cn',
  5. getName: funciton(){
  6. return '我的ID' + this.name;
  7. }
  8. }
  9. //访问对象属性
  10. console.log(user.name,user.id);
  11. //访问带空格属性
  12. console.log(user['my email']);
  13. //访问对象函数
  14. console.log(user.getName());

2.2.2 对象

  1. let user = {
  2. name,
  3. id:1,
  4. //此处可以省略funciton
  5. getName(){
  6. ruturn 'my name is' + this.name;
  7. }
  8. }
  9. name = '誓言';
  10. //当对象中有一个与共同作用域内的一个变量同名时在对象中可以直接引用

2.2.3 数组

正常情况下检查数组返回的是对象

使用Array.isArray(course)检测是否为数组

  • 数组是按照索引来访问的,从0开始
  1. let course = [1,'js','php'];
  2. //访问数组中的”js“
  3. console.log(course[1]);

2.2.3 函数

函数也是对象,对象是属性的无序集合,对象可以添加属性
```
function fun(){};
fun.email = ‘php@php.cn’;

```

总结

1. 原始类型

值传递,数值,字符串,布尔,undefined,null,Symbol

2. 引用类型

引用传递,对象,数组,函数

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!