10. Function
Object
Function
Object --
* JS最最核⼼心的数据类型和特殊概念
* A collection of named values (‘value’: any primitive
datatypes, or reference to any objects)
* ~= hash table
* ~= dictionary
* != class instance
* Pass by reference (~= pointer)
* 除 primitive type之外,万物皆为object
* 两种创建⽅方法:Literal(字⾯面量/直接
量)、‘new’操作符
Primitive datatype
11. Function
Object
Function
Function --
* 包含 executable code 的 object
* Named function 或 Anonymous function
* ⽤用字⾯面量创建时,有 Function Declaration 和
Function Expression 两种⽅方式
* ⽤用法:structured programming (like C), late binding
的 object method、constructor、创建 lexical
scope(词法作⽤用域)、Currying、传递 block (like
Ruby)、元编程、……
* 某些 built-in / host 对象的 executable code 是
native code
Primitive datatype
12. Function
Object
Function
Primitive datatype --
* 3 + 1:number、string、boolean + undefined
* null 是 object,Infinity、NaN 等都是 number
* Pass by value
* 前三种有 “Wrapper Class” (like Java),但 100% 情
况下都只使⽤用字⾯面量,Wrapper的价值是众多原⽣生
⼯工具函数(静态⽅方法)
Primitive datatype
22. this 是 late binding 的
(new Card().contents)() 时,
this 指向新对象,
(false || new Card().contents)() 时,
this 指向 Global Object
⽤用 new 调⽤用 Card 时,在进⼊入 Card
函数上下⽂文的阶段,this 被指向复制
出来的新对象
Card 和 contents 被作为属性(property)形式的
引⽤用值被调⽤用时,this 指向属性所属的对象,当
作为 Identifier (⽐比如变量名)形式的引⽤用值或实
际值被调⽤用时,this 被默认填充为 Global Object