码迷,mamicode.com
首页 > Web开发 > 详细

JS对象

时间:2019-07-08 21:03:37      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:this   json   parse   ice   包含   evel   bad   script   his   

技术图片

1.对象属性

  • writable 是否可写

  • enumerable 是否可枚举

  • configurable 是否可重新设置

  • value 属性值
//
var person = { x : 1 }
console.log(Object.getOwnPropertyDescriptor(Object,‘prototype‘));//Object {value: Object, writable: false, enumerable: false, configurable: false}
console.log(Object.getOwnPropertyDescriptor(person,‘x‘));//Object {value: 1, writable: true, enumerable: true, configurable: true}
Object.defineProperty(person,‘y‘,{value:2});
console.log(Object.getOwnPropertyDescriptor(person,‘y‘));//Object {value: 2, writable: false, enumerable: false, configurable: false}

2.对象方法

  • get/set
var man = {
    name: ‘Bosn‘,
    weibo: ‘@Bosn‘,
    get age() {
        return new Date().getFullYear() - 1994;
    },
    set age(val) {
        console.log(‘Age can\‘t be set to ‘ + val);
    }
}
console.log(man.age); // 25
man.age = 100; // Age can‘t be set to 100
console.log(man.age); // 25
  • hasOwnProperty
    hasOwnProperty判断是否为对象本身的属性。
function foo(){}
foo.prototype.z = 3;
var obj = new foo();
obj.x = 1;
obj.y = 2;

‘z‘ in obj; // true
obj.hasOwnProperty(‘z‘);// false
delete obj.z; // true
console.log(obj.z); // 3 原型上的属性不能被删除,但不会报错
delete obj.x; // true
console.log(obj.x); // undefined
  • Object.create(obj)

Object.create(obj)是系统内置函数,参数obj为一个对象,其会返回一个新的对象,新对象的原型指向参数obj。

var obj = Object.create({x:1});
obj.x //1
typeof obj.toString; // "function"
obj.hasOwnProperty(‘x‘); // false

var obj = Object.create(null);
typeof obj.toString; // undefined
  • getOwnPropertyDescriptor()
delete Object.prototype; // false

var descriptor = Object.getOwnPropertyDescriptor(Object, ‘prototype‘);//获取Object的prototype的属性,当第二个参数不存在与第一个参数对象中,返回undefined
console.log(descriptor.configurable); // false
  • propertyIsEnumerable()是否可枚举
var cat = new Object();
cat.legs = 4;

cat.propertyIsEnumerable(‘legs‘); // true
cat.propertyIsEnumerable(‘toString‘); // false
  • defineProperty()自定义对象的属性

    Object.defineProperty(cat, ‘price‘, {enumberable:false,value:1000});//设置对象cat的属性price为不可枚举,并赋值为1000
    cat.propertyIsEnumerable(‘price‘);//false
    cat.hasOwnProperty(‘price‘);//true
  • defineProperties 定义多个属性
var person = {};
Object.defineProperties(person, {
    title: { value: ‘fe‘, enumerable: true },
    salary: { value: 50000, enumerable: true, writable: true},
    luck: {
        get: function() {
            return Match.random() > 0.5 ? ‘good‘ : ‘bad‘;
        }
    },
    promote:{
        set: function(level){
            this.salary *= 1 + level * 0.1
        }
    }
});
console.log(person.salary);//50000
person.promote = 2;
console.log(person.salary); //60000

3.对象标签

  • __proto__

  • class标签
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
  • extensible标签:对象是否可扩展,即对象上的属性是否可以继续添加。
var obj = {x : 1, y : 2};
    console.log(Object.isExtensible(obj));//true 默认值为true
    Object.preventExtensions(obj);//禁止对象属性扩展
    console.log(Object.isExtensible(obj));//false
    obj.z = 1;
    console.log(obj.z);//undefined 设置失败
    console.log(Object.getOwnPropertyDescriptor(obj,‘x‘));//Object {value: 1, writable: true, enumerable: true, configurable: true}

    Object.seal(obj);//将configurable设置为false
    console.log(Object.getOwnPropertyDescriptor(obj,‘x‘));//Object {value: 1, writable: true, enumerable: true, configurable: false}
    console.log(Object.isSealed(obj));//true

    Object.freeze(obj);//将writable和configurable设置为false
    console.log(Object.getOwnPropertyDescriptor(obj,‘x‘));//Object {value: 1, writable: false, enumerable: true, configurable: false}
    Object.isFrozen(obj);//true

4.对象序列化

  • JSON.stringify()JSON.parse()
//
var obj = {x: 1, y: true, z: [1,2,3], nullVal: null};
console.log(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":"2015-01-20T14:15:43.910Z"}"
// 值为undefined的会被忽略,NaN转换为null,Infinity转换为null

obj = JSON.parse(‘{"x" : 1}‘);//属性x需被双引号包含着
obj.x; // 1

自定义序列化

var obj = {
    x : 1,
    y : 2,
    o : {
            o1 : 1,
            o2 : 2,
            toJSON : function () { //toJSON不能变
                    return this.o1 + this.o2;
            }
    }
};
console.log(JSON.stringify(obj)); // "{"x":1,"y":2,"o":3}"

其他对象方法

var obj = {x : 1, y : 2};
console.log(obj.toString()); // "[object Object]"
obj.toString = function() {return this.x + this.y};
console.log("Result " + obj); // "Result 3", by toString

console.log(+obj); // 3, from toString

obj.valueOf = function() {return this.x + this.y + 100;};
console.log(+obj); // 103, from valueOf

console.log("Result " + obj); // still "Result 3"
//valueOf与toString同时存在的时候,先调用valueOf,如果valueOf返回值不是基本类型,再调用toString,若toString返回不是基本类型,则报错.

JS对象

标签:this   json   parse   ice   包含   evel   bad   script   his   

原文地址:https://blog.51cto.com/11569511/2418303

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!