zoukankan      html  css  js  c++  java
  • JS

    栗子:

    var obj = {
      "name": "Poly",
      "career": "it"
    }
    Object.defineProperty(
    obj, "age", 
    { value:"forever 18", enumerable:false }
    );
    Object.prototype.protoPer1 = function(){console.log("proto");};
    Object.prototype.protoPer2 = 2;
    

    方法一 for in

    用于遍历对象的可枚举属性,包括自有属性、继承自原型的属性(不含Symbol属性)

    for(var key in obj){
      console.log(key);
    }
    //输出:name, career, protoPer1, protoPer2
    

    方法二 Object.keys(xx)/Object.values(xx)

    返回一个数组,元素均为对象自有的可枚举属性(不含继承的、Symbol属性)

    console.log(Object.keys(obj));
    console.log(Object.values(obj));
    //输出:["name", "career"]
    //输出:[ "Poly", "it"]
    

    Object.getOwnProperty

    用于返回对象的自有属性,包括可枚举和不可枚举的

    console.log(Object.getOwnPropertyNames(obj));
    //输出:["name", "career", "age"]
    
  • 相关阅读:
    [NOI2014]动物园
    2018.7.15模拟赛
    2018.7.13模拟赛
    [CodeForces]920F SUM and REPLACE
    [BZOJ3211]花神游历各国
    [GSS5] Can you answer these queries V
    [SPOJ1716] GSS3
    [HNOI2012]排队
    2018.7.10模拟赛
    7.3模拟赛
  • 原文地址:https://www.cnblogs.com/sunidol/p/11463600.html
Copyright © 2011-2022 走看看