1.JavaScript's Type System
JavaScript has only six types: undefined, null Boolean, String, Number, and Object.1.1 Static versus Dynamic
static: at compile time
dynamic: at runtime
Object foo = "abc";
The static type of foo is Object; its dynamic type is String.
JavaScript is dynamically typed; types of variables are generally not known at compile time.
coerce: implicit type conversion
2. Primitive Values versus Objects
(1) The primitive values are booleans, numbers, strings, null and undefined.(2) All other values are objects.
The most common kinds of objects are: palin objects/ arrays/ regex
Objects have the following characteristics: Compared by reference/ Mutable by default/ User-extensible
Changing undefined:
if (x === void 0) // always safe
3. Wrapper Objects
The three primitive types boolean, number, and string have corresponding constructors: Boolean, Number, String. Their instance (so-called wrapper objects) contain (wrap) primitive values. The constructors can be used in two ways:(1) As constructors:
> typeof new String('abc')
'object'
> new String('abc') === 'abc'
false
(2) As functions:
> String(123)
'123'
It is considered a best practice to avoid wrapper objects
Primitives Borrow Their Methods from Wrappers
> 'abc'.charAt == String.prototype.charAt
true
没有评论:
发表评论