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.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值