zoukankan      html  css  js  c++  java
  • ES5 方法学习

    Object


    1. Object.getPrototypeOf(o)
    获取对象的prototype对象。等价于以前的o.__proto__

    var o = {};
    Object.getPrototypeOf(o) === o.__proto__; // true

    2. Object.getOwnPropertyNames(o)
    获取自有属性名列表。结果列表将不包含原型链上的属性。

    var o = { bar: 42, a: 2, b: 3};
    Object.getOwnPropertyNames(o); // ["bar", "a", "b"]
    
    var o = {}
    o.__proto__.b = 2;
    Object.getPrototypeOf(o).c = 3; // 和上面的一句等价
    Object.getOwnPropertyNames(o); // []

    3. Object.keys

    返回对象o的所有可枚举(enumerable)属性的名称。
    和 Object.getOwnPropertyNames 区别如下:

    var o = {};
    // 属性 b 不可枚举
    Object.defineProperty(o, 'b', {
      value: 1
    });
    o.b; // 1
    Object.keys(o); // []
    Object.getOwnPropertyNames(o); // ["b"]

    4. Object.create(proto[, propertiesObject])
    The Object.create() method creates a new object with the specified prototype object and properties.
    第1个参数是该对象的 prototype, 第2个参数和 Object.defineProperties 第2个参数类似
    详见链接;

    5. Object.assign
    有点像 $.extend 方法, 但是是浅复制

    // 复制
    var o = {a: 1};
    var copy = Object.assign({}, o);
    copy.a; // 1
    copy.a = 2;
    copy.a; // 2
    
    // 复制
    var o1 = {a: 1};
    var o2 = {b: 2};
    var copy = Object.assign({}, o1, o2);
    copy.a; // 1
    copy.b; // 2
    
    // 浅复制
    var o = {a: {b: 1}};
    var copy = Object.assign({}, o);
    copy.a.b; // 1
    copy.a.b = 2;
    o.a.b; // 2
    
    // 只复制 可枚举的
    var obj = Object.create({ foo: 1 }, { // foo is on obj's prototype chain.
      bar: {
        value: 2  // bar is a non-enumerable property.
      },
      baz: {
        value: 3,
        enumerable: true  // baz is an own enumerable property.
      }
    });
    var copy = Object.assign({}, obj);
    copy.bar; // undefined;
    copy.baz; // 3

    5. Object.prototype.isPrototypeOf(v)

    检查对象是否是位于给定对象v的原型链上。

    var o = {};
    var q = Object.create(o);
    o.isPrototypeOf(q);

    6. Object.defineProperty(obj, prop, descriptor)

    obj 对象, prop 属性名, descriptor 属性值和描述
    详见链接;

    7. Object.defineProperties(obj, props)

    根据对象描述props来定义对象o,通常props包含多个属性的定义。
    比 Object.defineProperty 更实用, 因为可以定义多个属性

    var obj = {};
    Object.defineProperties(obj, {
      'property1': {
        value: true,
        writable: true
      },
      'property2': {
        value: 'Hello',
        writable: false
      }
      // etc. etc.
    });

    8. Object.getOwnPropertyDescriptor(o,p)

    获取对象描述

    The Object.getOwnPropertyDescriptor() method returns a property descriptor for an own property (that is, one directly present on an object and not in the object's prototype chain) of a given object.

    var o = { get foo() { return 17; } };
    var d = Object.getOwnPropertyDescriptor(o, 'foo');
    // d is {
    //   configurable: true,
    //   enumerable: true,
    //   get: /*the getter function*/,
    //   set: undefined
    // }
    
    var o = { bar: 42 };
    var d = Object.getOwnPropertyDescriptor(o, 'bar');
    // d is {
    //   configurable: true,
    //   enumerable: true,
    //   value: 42,
    //   writable: true
    // }

    9. Object.seal(o)

    seal 单词的意思是
    n. 印章,海豹
    v. 封闭,密封

    The Object.seal() method seals an object, preventing new properties from being added to it and marking all existing properties as non-configurable. Values of present properties can still be changed as long as they are writable.

    设置以后不可添加新属性和修改已有属性的特性,但是可以修改已有的属性

    var o = {a: 1, b:2};
    var obj = Object.seal(o);
    obj === o; // true, 注意它们是全等的
    Object.isSealed(obj); // true
    Object.isFrozen(obj); // false
    o.c = 222; 
    o.c; // undefined
    o.a = 2;
    o.a; // 2
    
    Object.defineProperty(obj, 'ohai', {
      value: 17
    }); // Uncaught TypeError: Cannot define property:ohai, object is not extensible.
    
    Object.defineProperty(obj, 'a', {
      value: 17
    });
    o.a; // 17
    
    Object.defineProperty(obj, 'a', {
      writable: true
    }); // Uncaught TypeError: Cannot redefine property: a
    
    // 这样不会报错, 因为没有修改
    Object.defineProperty(obj, 'a', {
      writable: false
    });

    10. Object.isSealed(o);
    判断一个对象是否sealed

    var obj = {};
    Object.defineProperties(obj, {
      'property1': {
        configurable: false
      }
    });
    Object.preventExtensions(obj);
    Object.isSealed(obj); // true

    11. Object.freeze(o)

    和 Object.seal 限制一样,并且还不能修改原来的属性

    var o = {a: 1, b:2};
    var obj = Object.freeze(o);
    obj === o; // true, 注意它们是全等的
    Object.isSealed(obj); // true
    Object.isFrozen(obj); // true
    obj.a = 22;
    obj.a; // 1
    obj.c = 22;
    obj.c; // undefined;
    Object.isFrozen(obj); // true
    Object.defineProperty(obj, 'a', {
      writable: true
    }); // Uncaught TypeError: Cannot redefine property: a

    12. Object.isFrozen(o)

    判断一个对象是否 frozen

    var obj = {};
    Object.defineProperties(obj, {
      'property1': {
        configurable: false,
        writable: false
      }
    });
    Object.preventExtensions(obj);
    Object.isFrozen(obj); // true

    13. Object.preventExtensions(o)

    将对象置为不可扩展。

    var obj = {};
    var o = Object.preventExtensions(obj);
    o === obj;
    o.a = 1;
    o.a; // undefined
    Object.isExtensible(o); // true

    14. Object.isExtensible(o)

    判断一个对象是否可扩展, 默认为 false

    15. Object.prototype.propertyIsEnumerable(p)

    检查一个对象上的属性p是否可枚举。

    var o = {}
    Object.defineProperties(o, {
        a: {
           enumerable: false
        },
        b: {
           enumerable: true
        }
    });
    o.propertyIsEnumerable('a'); // false
    o.propertyIsEnumerable('b'); // true

    16. Object.getOwnPropertySymbols

    待描述;

    17. Object.is

    判断2个值是否相等

    NaN == NaN; // false
    NaN === NaN; // false
    Object.is(NaN, NaN); // true
    
    // Special Cases
    Object.is(0, -0);            // false
    Object.is(-0, -0);           // true
    Object.is(NaN, 0/0);         // true

    Array


    详情请点链接

    String


    1. String.prototpye.trim

    去掉字符串两头的空白符和换行符。

    2. 字符订阅

    //property access on strings
    "abc"[1] === "b"; // 相当于 "abc".charAt(1)

    Function


    Function.prototype.bind(thisTarget, arg1,…argn)

    JSON


    JSON.parse(text)
    JSON.stringify(obj)

    Date


    1. Date.now
    获取当前时间距1970.1.1 00:00:00的毫秒数。
    Date.now(); //1492351123908

    2. Date.prototype.toISOString
    根据ISO860123生成时间字符串。
    (new Date).toISOString(); // "2017-04-16T09:01:23.366Z"

    参考链接:

    http://pij.robinqu.me/JavaScript_Core/ECMAScript/es5.html

    MDN Object

  • 相关阅读:
    PAT (Advanced Level) 1086. Tree Traversals Again (25)
    PAT (Advanced Level) 1085. Perfect Sequence (25)
    PAT (Advanced Level) 1084. Broken Keyboard (20)
    PAT (Advanced Level) 1083. List Grades (25)
    PAT (Advanced Level) 1082. Read Number in Chinese (25)
    HDU 4513 吉哥系列故事――完美队形II
    POJ Oulipo KMP 模板题
    POJ 3376 Finding Palindromes
    扩展KMP
    HDU 2289 Cup
  • 原文地址:https://www.cnblogs.com/zhengming2016/p/6720245.html
Copyright © 2011-2022 走看看