JavaScript 简明教程


引子

在数学中,函数是两个集合之间的二元关系,它与第一个集合的每个元素正好关联第二个集合的一个元素。典型的例子是从整数到整数,或从实数到实数的函数。

In mathematics, a function[note 1] is a binary relation between two sets that associates to each element of the first set exactly one element of the second set. Typical examples are functions from integers to integers, or from the real numbers to real numbers.

由于计算机直接从数学中得到启示,因此,3种事物是最重要的:

  • primitive expressions, which represent the simplest entities the
    language is concerned with, 在函数关系中的primitive本质上是数。

  • means of combination, by which compound elements are built
    from simpler ones, and, 如何表示函数。

  • means of abstraction, by which compound elements can be named
    and manipulated as units,函数如何抽象。

数据结构

JavaScript中共有8种数据类型。编程语言都有内置的数据结构,但不同语言的数据结构往往不同。本文试图列出JavaScript中可用的内置数据结构及其属性。这些可用于构建其他数据结构。尽可能与其他语言进行比较。

动态类型

JavaScript是一个 松散类型和 动态 语言。JavaScript中的变量与任何特定的值类型没有直接关联,任何变量都可以被分配(和重新分配)所有类型的值。

数据类型和结构类型

The latest ECMAScript standard defines nine types:

元语类型(6种)

Six Data Types that are primitives, checked by typeof operator:

  • undefined(未定义) : typeof instance === "undefined"

  • Boolean : typeof instance === "boolean"

  • Number : typeof instance === "number"

  • String(展示) : typeof instance === "string"

  • BigInt(任意精度整数) : typeof instance === "bigint"

  • Symbol(唯一匿名值) : typeof instance === "symbol"

结构类型(两种)

  • Object : typeof instance === "object". Special non-data but Structural type for any constructed object instance also used as data structures: new Object, new Array, new Map, new Set, new WeakMap, new WeakSet, new Date and almost everything made with new keyword; 在国内通常称为引用类型。

  • Function : a non-data structure, though it also answers for typeof operator: typeof instance === "function". This is merely a special shorthand for Functions, though every Function constructor is derived from Object constructor.

结构根元语(一种)Structural Root Primitive

  • null : typeof instance === "object". Special primitive type having additional usage for its value: if object is not inherited, then null is shown;

记住typeof操作符唯一有价值的目的和用法是检查数据类型。如果我们想检查从Object派生的任何结构类型,那么使用typeof是毫无意义的,因为 将始终接收“对象”。检查我们使用的对象类型的正确方法是 的实例 关键字。但即便如此,也可能存在误解。
我们可以看到,除了undefined和null几乎相同之外,每个基元类型的含义都是显而易见的。这是因为时间的概念与算法的目的紧密相连。我们可以宣称一些还不存在的东西 或者不存在 不再:未定义。但是,当我们希望能够表示存在的东西是空的,我们必须发明另一个关键字。这就是null所代表的:结构意义的开始。

尽管JavaScript声明时不需要类型声明(let, var, const声明),JavaScript是一个 松散类型和 动态 语言。通过typeof可知类型。