标签:间接 .class code 对象序列化 property class json对象 prototype val
不能直接获取class标签,通过tostring间接获取。
toString() 返回 "[object type]",其中type是对象的类型。
var toString = Object.prototype.toString;
function getType(o){return toString.call(o).slice(8,-1);}
toString.call(null);//‘[object Null]‘
getType(null);//‘Null‘
getType(undefined);//‘Undefined‘
getType(1);//‘Number‘
getType(new Number(1));//‘Number‘
typeof new Number(1);//‘object‘
getType(true);//‘Boolean‘
getType(new Boolean(true));//‘Boolean‘
var obj = {x:1,y:2};
Object.isExtensible(obj);//true,判断对象是否可拓展
Object.preventExtensions(obj);//阻止拓展
Object.isExtensible(obj);//false
obj.z = 1;
obj.z;//undefined
Object.getOwnPropertyDescriptor(obj,‘x‘);
//Object{value: 1, writable: true, enumerable: true, configurable: true}
Object.seal(obj);
//preventExtensions的基础上再将configurable设置为false
Object.isSealed(obj);//true,判断是否被seal。
Object.freeze(obj);
//seal的基础上再将writable设置为false
Object.isfrozen(obj);//true,判断是否被freeze。
==不影响原型链的属性==
var obj = {x:1,y:true,z:[1,2,3],nullVal:null};
//供后台使用
JSON.stringify(obj);//"{"x":1,"y":true,"z":[1,2,3],"nullVal":null}"
obj = {val:undefined,a:NaN,b:Infinity,c:new Date()};
JSON.stringify(obj);
/// "{"a":null,"b":null,"c":"2018-06-06T12:12:48.643Z"}"
obj = JSON.parse(obj);//解析json对象为js对象
obj.x;//1
==注==:如果属性值为Infinity或NaN,json化会转为null;值为Date对象,会转为UTC时间格式,属性值为undefined,属性不会出现在json中。
标签:间接 .class code 对象序列化 property class json对象 prototype val
原文地址:https://www.cnblogs.com/y-dt/p/9381608.html