@JunQiu
2018-09-18T21:02:34.000000Z
字数 4250
阅读 1330
summary_2018/07
language_js
The usual definition for primitives and objects in JavaScript is: The following values are primitive.
- Strings: "abc"
- Numbers: 4, 3.57 (all numbers in JavaScript are floating point)
- Booleans: true, false
- null: usually explicitly assigned
- undefined: usually a default value, automatically assigned
- All other values are objects.
Objects can be partitioned further:
1、Wrappers for primitives: Boolean, Number, String. Rarely used directly.
2、Creatable by literals. The following literals produce objects that can also be created via a constructor. Use literals whenever you can.
[] is the same as new Array()
{} is the same as new Object()
function() {} is the same as new Function()
/\s*/ is the same as new RegExp("\\s*")
Dates: new Date("2011-12-24")
1.1、Objects are mutable by default:(默认对象是可变)
> var obj = {};
> obj.foo = 123; // write
123
> obj.foo // read
123
1.2、Objects have unique identities and are compared by reference: Every object you create via an expression such as a constructor or a literal is considered different from every other object; a fact that can be observed via the equality operator (===). That operator compares objects by reference: two objects are only equal if they have the same identity. It does not matter whether they have the same content or not.(object或引用类型,比较的是地址是否相同(==/===))
> {} === {}
false
> var obj = {};
> obj === obj
true
1.3、Variables hold references to objects: Thus, two variables can refer to the same object – changes you make via one variable can be observed via the other variable.
> var var1 = {};
> var var2 = var1;
> var1.foo = 123;
123
> var2.foo
123
As expected, primitives are different:
2.1、Primitives are immutable: any property you add will be immediately forgotten.
> var str = "abc";
> str.foo = 123; // write - ignored
123
> str.foo // read
undefined
2.2、Primitives are compared by value, they don’t have individual identities: To compare two primitives, one looks at their values, their content. If their values are the same then they are considered equal.(原始数据类型比较值的大小)
> "abc" === "abc"
true
That means that the identity of a primitive is its value, it does not have an individual identity.
1.1、The three primitive types string, number and boolean have corresponding types whose instances are objects: String, Number, Boolean. They are sometimes called wrapper types and converting between primitive and wrapper is simple:(原始数据类型及包装类的转换)
Primitive to wrapper: new String("abc")
Wrapper to primitive: new String("abc").valueOf()
1.2、Primitive values such as "abc" are fundamentally different from wrapper instances such as new String("abc"). For example (typeof and instanceof are explained below):
> typeof "abc"
'string'
> typeof new String("abc")
'object'
> "abc" instanceof String
false
> new String("abc") instanceof String
true
> "abc" === new String("abc")
false
There are two ways that this borrowing is done.
1、The old way is to convert a primitive to a wrapper, on the fly.
2、The new way (via ECMAScript 5 strict mode) is to transparently use the methods from the wrapper’s prototype. The following code illustrates the difference [inspired by “The Secret Life of JavaScript Primitives”].
// Methods in Object.prototype are available to all primitives
Object.prototype.getType = function() {
return typeof this;
};
Object.prototype.getTypeStrict = function() {
"use strict";//严格透明
return typeof this;
};
console.log("".getType()); // object
console.log("".getTypeStrict()); // string
### js的装箱与拆箱
把基本数据类型转换对应的包装类型的操作称为装箱,把包装类型为对应的基本数据类型称为拆箱。
Tips:引用类型与基本包装类型(也是引用类型)的主要区别就是对象的生命周期。使用new操作符创建的引用类型的实例,在执行流离开当前作用域之前都一直保存在内存中。而自动创建的基本包装类型的对象,只存在于这一行代码的执行期(瞬间),然后立即销毁。这就意味着我们不能在运行时为属性添加属性和方法。
typeof /s/ === 'function'; // Chrome 1-12 Non-conform to ECMAScript 5.1
typeof /s/ === 'object'; // Firefox 5+ Conform to ECMAScript 5.1