美文网首页
JS - 对象(5)

JS - 对象(5)

作者: sunorry | 来源:发表于2015-02-01 18:47 被阅读27次

对象标签,对象序列化

  • [[proto]]
  • [[class]]
  • [[extensible]]

原型标签 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 Bollean(true)); // "Boolean"

extensible标签

var obj = {x:1, y:2};
Object.isExtensible(obj); // true
Object.preventExtensions(obj);
Object.isExtensible(obj); // flase
obj.z = 1;
obj.z; // undefined, add new property failed
Object.getOwnPropertyDescriptor(obj, 'x');
// Object {value: 1, writable: true, enumerable: true, configurable: true}

Object.seal(obj);
Object.getOwnPropertyDescriptor(obj, 'x');
//Object {value: 1, writable: true, enumerable: true, configurable: false}
Object.isSealed(obj); // true

Object.freeze(obj);
Object.getOwnPropertyDescriptor(obj, 'x');
//Object {value: 1, writable: false, enumerable: true, configurable: false}
Object.isFrozen(obj); // true

// [caution] not affects prototype chain!!!

序列化

var obj = {
    x: 1,
    y: true,
    z: [1, 2, 3],
    nullVla: null
};
JSON.stringify(obj); // "{"x":1,"y":true, "z":[1,2,3], "nullVla": null}"

//如果值是 undefined ,则不会出现在序列化中
obj = {
    val: undefined,
    a: NaN,
    b: Infinity,
    c: new Date()
};
JSON.stringify(obj); // "{"a":null,"b":null, "c":"2015-02-01T18:35:39.434Z"}"

obj = JSON.parse('{"x":1}');
obj.x; //1

序列化-自定义

var obj = {
    x:1,
    y:2,
    o:{
        o1:1,
        o2:2,
        toJSON: function() {
            return this.o1 + this.o2;
        }
    }
};

JSON.stringify(obj); //"{"x":1, "y":2, "o":3}"

其他对象发法

var obj = {x:1, y:2};

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

+obj; // 3, from toString

obj.valueOf = function() {
    return this.x + this.y + 100;
};

+obj; // 103, from valueOf

//字符串拼接仍然用 toString
"Result " + obj; //still "Result 3"

相关文章

  • JS - 对象(5)

    对象标签,对象序列化 [[proto]] [[class]] [[extensible]] 原型标签 proto ...

  • 2019-11-27

    1.js 对象属性 2.js 对象方法 3.对象访问器 4.对象构造器 5.对象构造器加属性 6.对象方法 7.j...

  • 5 JS中对象

    8 JavaScript 对象对象由花括号分隔。在括号内部,对象的属性以名称和值对的形式 (name : valu...

  • js的基本语法

    1.helloworld 2.js的基本语法 3.js的对象 4.批量产生对象 5.批量生产对象2 6.松散的语法

  • 事件

    1、鼠标的移入移除js部分 2、鼠标事件js部分 3、事件对象js部分 4、键盘事件js部分 5、输入框事件js部...

  • 什么是AJAX

    AJAX的起源: IE 5 率先在 JS 中引入 ActiveX 对象(API),使得 JS 可以直接发起 HTT...

  • 实现拖拽

    原生js实现 HTML5原生实现:拖拽draggable属性、DataTranfers对象

  • JS面向对象精要(二)_函数

    JS面向对象精要(一)_原始类型和引用类型JS面向对象精要(二)_函数JS面向对象精要(三)_理解对象JS面向对象...

  • JS面向对象精要(三)_理解对象

    JS面向对象精要(一)_原始类型和引用类型JS面向对象精要(二)_函数JS面向对象精要(三)_理解对象JS面向对象...

  • JS面向对象精要(四)_构造函数和原型对象

    JS面向对象精要(一)_原始类型和引用类型JS面向对象精要(二)_函数JS面向对象精要(三)_理解对象JS面向对象...

网友评论

      本文标题:JS - 对象(5)

      本文链接:https://www.haomeiwen.com/subject/whalxttx.html