[关闭]
@JunQiu 2018-09-18T13:02:34.000000Z 字数 4250 阅读 555

Js_primitivesVersusObjects、比较运算符、typeof、Instanceof

summary_2018/07 language_js


1、日常工作


2、技术学习

  1. The usual definition for primitives and objects in JavaScript is: The following values are primitive.
  2. - Strings: "abc"
  3. - Numbers: 4, 3.57 (all numbers in JavaScript are floating point)
  4. - Booleans: true, false
  5. - null: usually explicitly assigned
  6. - undefined: usually a default value, automatically assigned
  7. - All other values are objects.
  8. Objects can be partitioned further:
  9. 1Wrappers for primitives: Boolean, Number, String. Rarely used directly.
  10. 2Creatable by literals. The following literals produce objects that can also be created via a constructor. Use literals whenever you can.
  11. [] is the same as new Array()
  12. {} is the same as new Object()
  13. function() {} is the same as new Function()
  14. /\s*/ is the same as new RegExp("\\s*")
  15. Dates: new Date("2011-12-24")
  1. 1.1Objects are mutable by default:(默认对象是可变)
  2. > var obj = {};
  3. > obj.foo = 123; // write
  4. 123
  5. > obj.foo // read
  6. 123
  7. 1.2Objects 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或引用类型,比较的是地址是否相同(==/===))
  8. > {} === {}
  9. false
  10. > var obj = {};
  11. > obj === obj
  12. true
  13. 1.3Variables 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.
  14. > var var1 = {};
  15. > var var2 = var1;
  16. > var1.foo = 123;
  17. 123
  18. > var2.foo
  19. 123
  20. As expected, primitives are different:
  21. 2.1Primitives are immutable: any property you add will be immediately forgotten.
  22. > var str = "abc";
  23. > str.foo = 123; // write - ignored
  24. 123
  25. > str.foo // read
  26. undefined
  27. 2.2Primitives are compared by value, they dont 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.(原始数据类型比较值的大小)
  28. > "abc" === "abc"
  29. true
  30. That means that the identity of a primitive is its value, it does not have an individual identity.
  1. 1.1The 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:(原始数据类型及包装类的转换)
  2. Primitive to wrapper: new String("abc")
  3. Wrapper to primitive: new String("abc").valueOf()
  4. 1.2Primitive values such as "abc" are fundamentally different from wrapper instances such as new String("abc"). For example (typeof and instanceof are explained below):
  5. > typeof "abc"
  6. 'string'
  7. > typeof new String("abc")
  8. 'object'
  9. > "abc" instanceof String
  10. false
  11. > new String("abc") instanceof String
  12. true
  13. > "abc" === new String("abc")
  14. false
  1. There are two ways that this borrowing is done.
  2. 1The old way is to convert a primitive to a wrapper, on the fly.
  3. 2The new way (via ECMAScript 5 strict mode) is to transparently use the methods from the wrappers prototype. The following code illustrates the difference [inspired by The Secret Life of JavaScript Primitives”].
  4. // Methods in Object.prototype are available to all primitives
  5. Object.prototype.getType = function() {
  6. return typeof this;
  7. };
  8. Object.prototype.getTypeStrict = function() {
  9. "use strict";//严格透明
  10. return typeof this;
  11. };
  12. console.log("".getType()); // object
  13. console.log("".getTypeStrict()); // string
  14. ### js的装箱与拆箱
  15. 把基本数据类型转换对应的包装类型的操作称为装箱,把包装类型为对应的基本数据类型称为拆箱。
  16. Tips:引用类型与基本包装类型(也是引用类型)的主要区别就是对象的生命周期。使用new操作符创建的引用类型的实例,在执行流离开当前作用域之前都一直保存在内存中。而自动创建的基本包装类型的对象,只存在于这一行代码的执行期(瞬间),然后立即销毁。这就意味着我们不能在运行时为属性添加属性和方法。

  1. typeof /s/ === 'function'; // Chrome 1-12 Non-conform to ECMAScript 5.1
  2. typeof /s/ === 'object'; // Firefox 5+ Conform to ECMAScript 5.1

list


添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注