《你不知道的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();
    // ..
}
相关推荐
林深现海20 分钟前
Jetson Orin nano/nx刷机后无法打开chrome/firefox浏览器
前端·chrome·firefox
黄诂多33 分钟前
APP原生与H5互调Bridge技术原理及基础使用
前端
前端市界37 分钟前
用 React 手搓一个 3D 翻页书籍组件,呼吸海浪式翻页,交互体验带感!
前端·架构·github
文艺理科生38 分钟前
Nginx 路径映射深度解析:从本地开发到生产交付的底层哲学
前端·后端·架构
千寻girling39 分钟前
主管:”人家 Node 框架都用 Nest.js 了 , 你怎么还在用 Express ?“
前端·后端·面试
C澒1 小时前
Vue 项目渐进式迁移 React:组件库接入与跨框架协同技术方案
前端·vue.js·react.js·架构·系统架构
xiaoxue..1 小时前
合并两个升序链表 与 合并k个升序链表
java·javascript·数据结构·链表·面试
清山博客1 小时前
OpenCV 人脸识别和比对工具
前端·webpack·node.js
要加油哦~1 小时前
AI | 实践教程 - ScreenCoder | 多agents前端代码生成
前端·javascript·人工智能
程序员Sunday1 小时前
说点不一样的。GPT-5.3 与 Claude Opus 4.6 同时炸场,前端变天了?
前端·gpt·状态模式