初窥javaScrit权威指南,第六章对象(2)

来源:互联网 发布:淘宝店铺二维码在哪里 编辑:程序博客网 时间:2024/05/24 03:24

检测属性

检测属性:var o = {x:1}"x" in o;  // true: x是o的属性"y" in o;  // false: y不是o的属性"toString" in o; // true:继承了toString属性检测自有属性hasOwnPropertyvar o = inherit({y:2});o.x = 1;o.hasOwnProperty("x"); // true o有一个可以枚举的自有属性o.hasOwnProperty("y"); // false y是继承来的Object.hasOwnProperty("toString"); // false 继承来的加强版:自有且可枚举的:var o = inherit({y:2});o.x = 1;o.propertyIsEnumerable("x"); // true o有一个可以枚举的自有属性o.propertyIsEnumerable("y"); // false y是继承来的Object.prototype.propertyIsEnumerable("toString"); // false 不可枚举!== 不好o.x!== undefined;  // 属性存在,属性值是undefined| 属性不存在

枚举属性:

for in 来遍历属性。var o = {x:1,y:2,z:3};o.propertyIsEnumerable("toString"); // false 不可枚举for(p in 0){ console。log(p);       // toString不会遍历}--------------------------------------------------for(p in o){ if(!o.hasOwnProperty(p)) continue; //跳过继承的属性}for(p in o){ if(typeof o[p] === "function") continue; // 跳过方法}--------------------------------------------------extend 来遍历function mergeo,p){  for(prop in p){     o[prop] = p[prop]; // 为o添加属性}return o;}function restricto,p){  for(prop in o){     if(!(prop in p)) delete o[prop]; // 如果p中不存在 删除  }}。。。。在ECMAScript5提供两个更好的方案:Object.keys() 返回一个数组 可枚举的自有属性Object。getOwnPropertyNames()  自有属性

getter and setter

ECMAScript5中:var o = { data_prop : value, //存取器 get accessor_prop() {/*函数体*/}, set accessor_prop() {/*函数体*/}};如:var p = {  x: 1,  y: 2,get r() {return Math.sqrt(this.x*this.x + this.y*this.y)},set r(newvalue) {  var oldvalue = Math.sqrt(this.x*this.x+this.y*this.y);  var radio = newvalue / oldvalue;  this.x *= radio;  this.y *= radio;}};var q = inherit(p);q.x = 1,q.y=2;console.log(q.r); // r是继承而来的

属性的特性

4个:值,可写性,可枚举性,可配置性。getOwnPropertyDescriptor()获得某个对象的特定属性描述符/* { value: 1, writable: true, enumerable: true,configurable: true } */Object.getOwnPropertyDescriptor({x:1},"x");// undefinedconsole.log(Object.getOwnPropertyDescriptor({},"octet"));// undefined 继承属性console.log(Object.getOwnPropertyDescriptor({},"toString"));想要获得继承的特性,需要遍历原型链Object.getProperty()设置属性的特性,或者想让新建属性具有某种特性,则需要调用Object。defineProperty()var o = {};Object.defineProperty(o,"x",{value:1,writable : true,enumerable : true,configurable: true})})o.x; // 1Object.keys(o); // []Object.defineProperty(o,"x",{wriable:false});o.x = 2; // 操作失败但不报错,严格模式下抛出类型错误异常//属性依旧是可以配置的Object.defineProperty(o,"x",{get: function(){return 0;}});o.x; // 0新创建的对象默认都是 falseundefined

对象的三个属性

原型属性:Object.create(xx);ECMAScript5中 用Object。getPropertypeOf()来查询它的原型var p = {x:1};var o = Object.creat(p);p.isPropertyOf(o)   // true: o继承了pObject.prototype.isPrototypeOf(o) // true :p继承自Object.prototype
类型属性:classOf()函数返回传递给它的任意对象的类function classof(o) {    if(o === null) return "Null";    if(o === undefined) return "Undefined";    return Object.prototype.toString.call(o).slice(8,-1);}如:classof(null)  // "Null"classof(1)  // "Number"classof("")  // "String"classof({})  // "Object"classof([])  // "Array"classof(false)  // "Boolean" classof(/./)  //  "Regexp"classof(new Date())  // "Date"classof(window)  // "Window" 客户端宿主对象function f(){}   // 定义一个自定义构造函数classof(new f());  // "Object"
可扩展性:ECMAScript5  设置查询可扩展性的函数:Object.isExtensible(p) // 是否可扩展Object.preventExtensions(p) // 转换为不可扩展Object.seal(p) // 设置属性为不可配置,对象不可扩展,已有的可写属性可以设置Object.isSealed(p) // 是否封闭Object.freeze(p) // 冻结,设置属性为不可配置,对象不可扩展,自有的所有属性设置为只读(如果对象属性具有setter方法,那么存取器属性不受影响人就可以调用它)

序列化对象:

是指: 将对象的状态转化为字符串,也可将字符串还原为对象。ECMAScript 5 :o = {x:1,y:{z:[false,null,""]}};s = JSON.stringify(o); // '{"x":1,"y":{"z":[false,null,""]}}'p = JSON.parse(s); // p是o的深拷贝

对象的方法:

toString()toLocaleString() 本地化字符串toJSON()   (JSON.stingify 调用该方法)valueOf()   // 转化为某种原始值而非字符串类型
原创粉丝点击