JS-特殊类型

null and undefined:null is a language keyword that evaluates to a special value that is usually used to indicates the absense of a value.null can be thought of as a special object value that indicates 'no object'.

console.log(typeof null);//object
console.log(typeof undefined);//undefined

 undefined value is the value of variables that have not been initialized and the value you get when you query the value of an object property or array element that doen not exist.

Despite these differences, null and undefined both indicates an absence of value and can often be used interchangeably.Neither null nor undefined have any properties or methods.You might consider undefined to represent a system-level,unexpected,or error-like absence of value and null to represent program-level,normal,or expected absence of value.

 

Wrapper Objects:whenever you try to refer to a property of a string s, JS converts the string value to an object as if by calling new String(s).This object inherits string methods and is used to resolve the property reference.Once the property has been resolved,the newly created object is discarded.Numbers and Booleans have methods for the same reason that strings do.

var s = 'test';
s.len = 4;
var t = s.len;
console.log(t);//undefined

 The second line of code creates a temporary String object,sets its len property to 4,and then discard that object.The third line creates a new String object from the original(unmodified) string value and tries to read the len property.The change is made on a temporary object and does not persist.

 

Immutable Primitive Values and Mutable Object References:There is a fundamental difference in JS between primitive values and objects:Primitives are immutable.Objects are different than primitives.First,they are mutable-their values can change.Second,they are not compared by value.Two objects values are the same if and only if they refer to the same underlying object.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(js)