javascript

基本数据类型和包装类

demo:

var str='dsdsd';

var strObj=new String('string');

在基本数据类型访问对应包装类的属性时,javascript会把这个基本数据类型转换成对应的包装类,

在操作完成之后,这个临时对象会被销毁掉:

demo:

str.length   //6

str.t=4;//执行成功

console.log(str.t)//undefined

类型检测:

typeof   函数对象和基本数据类型 number boolean function string undefined      null Array返回Object

instanceof   obj instanceof Object 判断左边的数据原型链上是否有左边数据的prototype属性

Object.prototype.toString

constructor

duck type

typeof 适合基本类型及function检测,遇到null失效;

[[class]]

通过{}.toString拿到,适合内置对象和基本类型,遇到null和undefined失效(IE679等返回[object Object])

instanceof

适合自定义对象,也可以用来检测原生对象,在不同的iframe和window间检测时失效

逗号运算符:var temp=(3,4,5,6);//会运算所有的表达式,最后取最后一个值 temp=6; 很少见

delete obj.x    删除对象上的属性

Object.definePrototype(obj,x,{configurable:false,value;10})  定义对象上的属性不可删除

in 判断对象中是否有某个属性    'name' in obj

let obj={name:'zhangsan'}; 'name' in obj true  'age' in obj false; 

判断一个对象是否含有某个属性

obj.hasOwnProperty('属性名')

with 

让js引擎优化更难   可读性差 可被变量定义代替 严格模式下被禁用

with({name:'zhangsan'}){console.log(name)//zhangsan}

原文地址:https://www.cnblogs.com/xiaofenguo/p/11436207.html