JS基础 03-数据类型总览、输出方式

130 阅读1分钟

JS数据类型

  • 基本数据类型(值类型)

    • number 数字
    • string 字符串
    • boolean 布尔
    • null 空指针
    • undefined 未定义
    • symbol 唯一值
  • 引用数据类型

    • Object 对象
      • {} 普通对象
      • [] 数组对象
      • /^$/ 正则对象
      • Date 日期对象
      • Math 数学函数
    • Function 函数
// number
let n = 10;
n = NaN;
n = Infinity; //无穷大  -Infinity

// string
let str = '';
str = '19';
str = `name`;

// boolean
let bool = true;
bool = false;

// null、undefined
let noOne = null;
noOne = undefined;
let noOne;  // undefined

// symbol,每一个都是唯一值
let x = Symbol('A');
let y = Symbol('A');
x == y;  // false
// object 键值对
let obj = {
    name: 'AA',
    age: 10
};
obj.tall = 180;
obj['money'] = 100000;

// array
let arr = [10, 20 , '30', null];
arr.push(40);

// RegExp
let reg = /$[+-]?(\d|([0-9]\d+))(\.\d+)?^/;

// function
function fun(x, y) {
    let total = x + y;
    return total;
}

// es6 箭头函数
let fn = () =>{
    return 10;
}

输出方式

  • console.
    • log() // 控制台输出
    • error()
    • info()
    • dir() // 控制台详细输出
    • table() // 数据以表格形式输出
    • time() / timeEnd() // 计算程序消耗时间
    • warn() // 输出警告信息
  • window 提示框
    • alert() // 提示框,会阻碍主线程的渲染,默认转化成字符串 toString
    • confirm() // 确认取消提示,会阻碍主线程的渲染,默认转化成字符串 toString
    • prompt() // confirm 基础上增加提示,会阻碍主线程的渲染,默认转化成字符串 toString
  • 向内面指定容器中插入内容
    • document.write // 把内容写入到页面中,默认转化成字符串 toString
    • innerHTML / innerText // 向页面指定容器输出内容;默认转化成字符串 toString;把之前容器中的内容覆盖;追加 采用 += 的方式;
      • innerHTML // 识别标签并渲染
      • innerText // 全部都是文本
    • value // 给文本框赋值
console.time('AA');
for(let i = 0;i < 9999; 1++) {}
console.timeEnd('AA');  // AA: 0.2ms

alert([1, 2, 3])  // "1, 2, 3"
alert({name: 'AA'})  // 普通对象转换为字符串 "[object, object]"

let flag = confirm('yes?');
console.log(flag);  // yes: true, no: false

let reason = prompt('yes?');  // 信息输出
console.log(reason);  // yes: "信息输出", no: null
[1, 2].toString // 1, 2
{name: 'AA'}.toString  // [onject, object]
(function(){}).toString  // function(){}

珠峰培训 - 40个小时彻底打实JavaScript基础 P11、P12