Javascript Object要领详解

Object.create(o)

建立对象

Object.create({x: 1}) //建立一个平常对象
Object.create(null) //建立一个没有原型的新对象, 不继续任何属性和要领

Object.keys(o)

返回对象中可罗列的自我属性的称号的数组

Object.getOwnPropertyNames(o)

返回对象中一切自我属性的称号的数组

属性的特征

属性有两种特征, 数据属性和存取器属性
数据属性: {value: , writable, enumerable, configurable}
存取器属性: {get: , set, enumerable, configurable}

Object.getOwnPropertyDescriptor(o, prop)

能够获得某个对象特定自有属性的属性描述符

Object.getOwnPropertyDescriptor({x:1}, "x")

==>

Object {value: 1, writable: true, enumerable: true, configurable: true}

若想要猎取继续属性的属性描述符, 须要遍历原型链(Object.getProtytypeOf())

Object.defineProperty

Object.defineProperty({}, "x", {
                            value: 1, //值
                            writable: true,    
                            enumerable: true,
                            configurable: true
                        }
Object.defineProperty({}, {
    x: { value: 1, writable: true, enumerable: true, configurable: true},
    x: { value: 2, writable: true, enumerable: true, configurable: true},
    r: {
        get: function() { return Math.max(this.x, this.y)},
        enumerable: true, 
        configurable: true
    }
})

configuration = false 时为 不可设置
假如存取器属性是不可设置的, 则不能修正get和set, 也不能转换为数据属性
假如数据属性是不可设置的, 则不能转换为存取器属性
假如数据属性是不可设置的, 则不能将writable从false->true, 然则能够从true->false
假如数据属性是不可设置且为可写时, 则不能修正它的值, 但是可设置但不可写时, 值是能够修正的(现实上是先把可写性转成true, 修正值后, 再改成false)

完全复制属性特征的extend要领

Object.defineProperty(Object.prototype,
    "extend",
    {
        writable: true,
        enumerable: false,
        configurable: true,
        value: function(o){
            //猎取一切可罗列的自有属性
            var names = Object.getOwnPropertyNames(o);
            for(var i = 0; i < names.length; i++){
                if (names[i] in this) continue;
                //猎取属性描述符
                var desc = Object.getOwnPropertyDescriptor(o, names[i]);
                //定义属性
                Object.defineProperty(this, names[i], desc);
            }
        }
    }
);

注:
ECMAScript 5 之前的非标准要领
lookupGetter
lookupSetter
defineGetter
defineSetter

Object.isPrototypeOf()

查询原型
注: Firefox, Safari和Chrome支撑__proto__查询, IE和Opera不支撑

Object.esExtensible(o)

推断对象是不是是可扩大的

Object.preventExtensions(o)

把对象转为不可扩大的

Object.seal(o)

在preventExtensions的基础上, 将一切自有属性设置为不可设置的,即不能增添新属性, 且不能改已有属性, 能够用Object.isSealed来检测

Object.freeze(o)

对象凝结, 在seal的基础上, 将自有的一切数据属性设置为只读, 能够用Object.isFrozen()来检测

Object.assign

浅层复制, 重要用于对象的扩大

Object.prototype

平常只要function对象具有prototype属性
__proto__是原型链的现实指针

// 原型链的顶端
log((Object.prototype).__proto__);                                      // null
log(Function.prototype.__proto__ == Object.prototype);                  // true
log(Object.__proto__            == Function.prototype);                 // true
log(Function.__proto__          == Function.prototype);                 // true
log(Array.__proto__             == Function.prototype);                 // true
log(Company.__proto__           == Function.prototype);                 // true
log(Object.__proto__            == Function.prototype);                 // true
log(Company.prototype.__proto__ == Object.prototype);                   // true
log(c1.__proto__                == Company.prototype);                  // true

数据摘自 http://2660311.blog.51cto.com/2650311/1358226/

hasOwnProperty

用来检测给定的名字是不是是对象的自有属性, 关于继续的属性将返回false.

var o = {x: 1}
o.hasOwnProperty("x")        // true: o有个自有属性x
o.hasOwnProperty("y")        // false: o中不存在属性y
o.hasOwnProperty("toString") // false: toString是继续属性

in 能够辨别不存在的属性和存在但值为undefined的属性

var o = {x: undefined}
"x" in o // true, 属性存在
"y" in o //false, 属性不存在
delete o.x;
"x" in o //false, 已删除, 不存在

propertyIsEnumerable

是hasOwnProperty的增添版, 只要检测到是自有属性且可罗列(enumerable==true)时才返回true

var o = {x: 1}
o.propertyIsEnumerable("x") //true
Object.prototype.propertyIsEnumerable("toString") //false, 不可罗列

toString和toLocalString

一般来讲, toLocalString会直接挪用toString的值返回, 除了特别的几个对象外:
Date, Number 会做本地化的转换
Array, Array在做toLocalString的时刻会调每一个元素的toLocalString, 而非toString

valueOf

valueOf和toString要领异常相似, 只要当须要转换成对象的某种原始值, 而非字符串时才会挪用, 尤其是在Date和Number.

本文以进修为主, 重要资本来自于<<JavaScript威望指南(第6版)>>

    原文作者:jia
    原文地址: https://segmentfault.com/a/1190000005653355
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞