Blogger Information
Blog 94
fans 0
comment 0
visits 91870
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
【JS】js 数据类型 [超级干货]总结
可乐随笔
Original
695 people have browsed it

JS 数据类型

1.原始(5):数值、字符串、布尔、undefined、null
2.引用(3):数组、对象、函数

数组与对象的区别

  • 数组与对象并无本质的区别,仅仅是语法上的不同
  • 仅仅是成员声明方式和访问方式不同
  • 显然对象的语义化更好
  • 所以,可以将数组,看成对象的一个子集或特例

函数的本质

  • 函数是JS中,最重要的数据类型
  • 函数可视为一个普通的值
  • 函数可以充当返回值,参数等,凡是用到值的地方
  • 如果没有return,则返回 undefined

1.原始类型

  • 常用:number,string,boolean
  • 特殊:undefined,null
  • 查询:typeof
  • 特点:单值

(1). 数值类型:number

  1. // number
  2. console.log(123,typeof 123)

(2). 字符串类型:string

  1. //string
  2. console.log('php.cn',typeof 'php.cn')
  3. // string定界符:单引号/双引号,反引号 ``
  4. console.log(`Hello`)
  5. // 反引号字符串功能非常强大,不仅仅是字符串,更是一个“模板”
  6. let uname = '老马'
  7. console.log(`Hello,${uname}`)
  8. // `Hello,${uname}`:模板字面量,类似于PHP中的双引号
  9. // ${uname}: 插值,占位符

(3). 布尔类型 boolean

  1. console.log(true,typeof true)
  2. console.log(false,typeof false)

(4). undefined类型

  1. // undefined
  2. console.log(typeof a)

(5). 空类型

  1. // null
  2. console.log(typeof null)

2. 引用类型

  • 常用:array,object,function
  • 特点:多值

(1). 数组类型

  • 声明:[…]
  • 索引:[0,1,2,3,…] 从0开始递增的“有序”正整数
  • 值:可以是任何类型
  • 索引值:数组成员 或 数据元素
  1. const arr = ['手机', 5000, true]
  2. // 逐个:索引
  3. console.log(arr[0],arr[1],arr[2])
  4. console.log(typeof arr,Array.isArray(arr),Array.isArray(uname))

(2). 对象类型

  • 声明: 字面量 {…}
  • 索引:语义化字符串,功能与数据索引类似,但“无序”
  • 值:可以是任何类型
  • 属性+值:对象成员 或 对象元素
  1. const obj = {
  2. name:'手机',
  3. price: 5000,
  4. is_stock: true
  5. }
  6. console.log(obj['name'],obj['price'],obj['is_stock'])
  7. // 点语法,要求:属性名是合法的标识符
  8. console.log(obj.name,obj.price,obj.is_stock)
  9. console.log(typeof obj)

* (3). 函数类型

  1. //命名函数
  2. function fn1(){}
  3. //匿名函数
  4. const fn2 = function () {}
  5. //箭头函数:匿名函数的语法糖
  6. const fn3 = () => {}
  7. console.log(typeof fn1,typeof fn2, typeof fn3)

(4). 扩展类型

  1. // forEach: 用于迭代遍历数据组或对象
  2. // forEach(callback):参数是一个函数,当函数当参数时,叫“回调”
  3. // arr.forEach(function(item,key,arr){}),只有第一个参数item是必选
  4. console.log('-----扩展--------------')
1. 多维数组
  1. console.log('--------多维数组----------')
  2. const arr1 = [
  3. [1, '西瓜', 10],
  4. [2, '苹果', 20],
  5. [3, '黄桃', 30]
  6. ]
  7. // arr1.forEach(function (item){
  8. // console.log(item)
  9. // })
2. 箭头函数
  1. arr1.forEach( item => console.log(item))
  2. // ? 2. 对象数组
  3. console.log('--------对象数组-----------')
  4. const fruits = [
  5. { id: 1, name: '西瓜', price: 10},
  6. { id: 2, name: '苹果', price: 20},
  7. { id: 3, name: '黄桃', price: 30}
  8. ]
  9. fruits.forEach(item => console.log(item))
3. 类数组
  • 类数组特征
  • 1.由0开始递增的正整数的索引/属性
  • 2.必须要有 length,表示成员数量/数组长度
  1. // 不是class,类:类似,像,类数据->类似一个数组,但不是数组
  2. // 仍然是一个对象,用对象来模拟一个数组
  3. // DOM编程,浏览器中的对象
  4. console.log('--------类数组------------------')
  5. const likeArr = {
  6. 0: 'admin',
  7. 1: 'admin@qq.com',
  8. 2: '87867535',
  9. length:3,
  10. }
  11. console.log(typeof likeArr)
  12. // ikeArr.forEach(item => console.log(item))
  13. //将类数组转化为真正的数组
  14. console.log(Array.from(likeArr))
4. 函数数组

//数组成员是函数

  1. // const events =[
  2. // function () {
  3. // return '准备发射'
  4. // },
  5. // function () {
  6. // return '击中目标'
  7. // },
  8. // function () {
  9. // return '敌人投降'
  10. // }
  11. // ]
  12. //箭头函数简化
  13. const events = [() => '准备发射', () => '击中目标', () => '敌人投降']
  14. events.forEach( ev => console.log(ev()))
  15. // ? 对象方法
  16. //对象的方法,就是属性,只是它的值是一个匿名函数
  17. console.log('--------对象方法------------------')
  18. const user = {
  19. uname: '老马',
  20. email: 'nx99@qq.com',
  21. getUser: function () {
  22. return `${this.uname}: ${this.email}`
  23. }
  24. }
  25. //console.log(user.getUser())
  26. //改成数组
  27. const userArr = [
  28. '老马',
  29. 'nx99@qq.com',
  30. function () {
  31. return `${this[0]}: ${this[1]}`
  32. }
  33. ]
  34. console.log(userArr[2]())
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!