《你不知道的JavaScript-中卷》第一部分-类型和语法-笔记-1-类型

有人说,JavaScript 是唯一一门可以先用后学的编程语言。

1.2 内置类型

  • 空值(null)
  • 未定义(undefined)
  • 布尔值( boolean)
  • 数字(number)
  • 字符串(string)
  • 对象(object)
  • 符号(symbol,ES6 中新增)

除对象之外,其他统称为"基本类型"。

用 typeof 运算符来查看值的类型,它返回的是类型的字符串值

js 复制代码
typeof undefined === "undefined"; // true

typeof true === "boolean"; // true

typeof 42 === "number"; // true

typeof "42" === "string"; // true

typeof { life: 42 } === "object"; // true

// ES6中新加入的类型
typeof Symbol() === "symbol"; // true

神奇的null

  • 正确的返回结果应该是 "null",但这个 bug 由来已久,在 JavaScript 中已经存在了将近二十年,也许永远也不会修复
js 复制代码
typeof null === "object"; //true

判断null

js 复制代码
        var a = null;
        const isNull = (!a && typeof a === "object");//true
        console.log(isNull);

函数

  • function 是 object的子类型
  • 有一个内部属性 [[Call]],该属性使其可以被调用
  • 还有其他属性,length表示参数的个数
js 复制代码
console.log(typeof function () { } === "function");//true

function funa(a,b,c){}
console.log(funa.length);//3

数组

  • 数组的类型是"object"
  • 数组的length属性是元素的个数
js 复制代码
console.log(typeof [1, 2, 3] === 'object');

1.3 值和类型

JavaScript 中的变量是没有类型的,只有值才有。变量可以随时持有任何类型的值。

在对变量执行 typeof 操作时,得到的结果并不是该变量的类型,而是该变量持有的值的类型,因为 JavaScript 中的变量没有类型。

js 复制代码
        var a = 42;
        typeof a; // "number"
        a = true;
        typeof a; // "boolean"

1.3.1 undefined 和 undeclared

变量在未持有值的时候为 undefined。此时 typeof 返回 "undefined":

  • 已在作用域中声明但还没有赋值的变量,是 undefined 的。
  • 还没有在作用域中声明过的变量,是 undeclared 的。
  • typeof 对于未定义和未声明的变量,都返回"undefined"
js 复制代码
        var a;
        typeof a; // "undefined"
        var b = 42;
        var c;
        // later
        b = c;
        typeof b; // "undefined"
        typeof c; // "undefined"
        
        //未声明的
        d;//ReferenceError: b is not defined
        
        //但是 typeof 处理的很特殊
        typeof d;//"undefined"

1.3.2 typeof Undeclared

安全防范机制:确认是否有全局变量声明,而进行某些业务逻辑的处理

比如全局DEBUG标志

js 复制代码
// 这样会抛出错误
if (DEBUG) {
    console.log( "Debugging is starting" );
}

// 这样是安全的
if (typeof DEBUG !== "undefined") {
    console.log( "Debugging is starting" );
}

比如用户自定义函数(polyfill

js 复制代码
if (typeof atob === "undefined") {
    atob = function() { /*..*/ };
}

其他的常见做法,检查window对象属性

js 复制代码
if (window.DEBUG) {
    // ..
}

if (!window.atob) {
    // ..
}

检查外部函数是否存在,或使用默认实现

js 复制代码
function doSomethingCool() {
    var helper = (typeof FeatureXYZ !== "undefined") ? FeatureXYZ :function() { /*.. default  feature ..*/ };
    var val = helper();
    // ..
}

或将依赖通过参数显式地传递到函数中

js 复制代码
function doSomethingCool(FeatureXYZ) {
    var helper = FeatureXYZ || function() { /*.. default feature ..*/ };
    var val = helper();
    // ..
}
相关推荐
默_笙1 分钟前
🌀 别再手动写 Prompt 了!我让 AI 自己循环改到满意为止
javascript
PedroQue992 分钟前
Vite插件体系1.0.0:API稳定,生产就绪
前端·vite
用户059540174463 分钟前
把LLM记忆测试从手工脚本换成Pytest参数化,回归时间从2小时降到10分钟
前端·css
donecoding10 分钟前
3 条命令搞定闭环 Monorepo:Lerna 版本管理 + 拓扑构建 + 自定义分发
前端·前端框架·node.js
IT_陈寒17 分钟前
Vue的这个响应式陷阱让我熬到凌晨三点
前端·人工智能·后端
爱勇宝9 小时前
大多数人不是在使用 AI 赚钱,而是在帮 AI 公司赚钱
前端·后端·程序员
冬奇Lab10 小时前
每日一个开源项目(第143篇):page-agent - 纯 JS 的网页 GUI Agent,无需截图、无需插件、无需后端
前端·人工智能·agent
To_OC11 小时前
LC 994 腐烂的橘子:人人都说是 BFS 入门题,我却写了三遍才过
javascript·算法·leetcode
IT_陈寒14 小时前
React的这个渲染问题连官方文档都没说清楚
前端·人工智能·后端
追逐时光者16 小时前
别再满网找零散工具了,腾讯 QQ 浏览器这个“帮小忙”工具箱真能省时间
前端·后端