zoukankan      html  css  js  c++  java
  • js:方法3. 对象

    Object.constructor###

    object.constructor

    a = new Array(1,2,3); // Create an object
    a.constructor == Array // Evaluates to true
    

    Object.create()###

    Object.create(proto); Object.create(proto, descriptors)

    // Create an object that has own properties x and y and inherits property z
    var p = Object.create({z:0}, {
     x: { value: 1, writable: false, enumerable:true, configurable: true},
     y: { value: 2, writable: false, enumerable:true, configurable: true},
    });
    

    Object.defineProperties()###

    Object.defineProperties(o, descriptors)

    // Add read-only properties x and y to a newly-created object
    var p = Object.defineProperties({}, {
     x: { value: 0, writable: false, enumerable:true, configurable: true},
     y: { value: 1, writable: false, enumerable:true, configurable: true},
    });
    

    Object.defineProperty()

    Object.defineProperty(o, name, desc)

    function constant(o, n, v) { // Define a constant o.n with value v
     Object.defineProperty(o, n, { value: v, writable: false
     enumerable: true, configurable:false});
    }
    

    Object.getOwnPropertyDescriptor()

    Object.getOwnPropertyDescriptor(o, name)

    The descriptor for a data property looks like this:
    {
     value: /* any JavaScript value */,
     writable: /* true or false */,
     enumerable: /* true or false */,
     configurable: /* true or false */
    }
    The descriptor for an accessor property looks like this:
    {
     get: /* function or undefined: replaces the property value */,
     set: /* function or undefined: replaces the writable attribute */,
     enumerable: /* true or false */,
     configurable: /* true or false */
    }
    

    Object.getOwnPropertyNames()

    Object.getOwnPropertyNames(o)

    Object.getOwnPropertyNames([]) // => ["length"]: "length" is non-enumerable
    

    Object.getPrototypeOf()

    Object.getPrototypeOf(o)

    var p = {}; // An ordinary object
    Object.getPrototypeOf(p) // => Object.prototype
    var o = Object.create(p) // An object that inherits from p
    Object.getPrototypeOf(o) // => p
    

    Object.hasOwnProperty()###

    object.hasOwnProperty(propname)

    var o = new Object(); // Create an object
    o.x = 3.14; // Define a noninherited local property
    o.hasOwnProperty("x"); // Returns true: x is a local property of o
    o.hasOwnProperty("y"); // Returns false: o doesn't have a property y
    o.hasOwnProperty("toString"); // Returns false: toString property is inherited
    

    Object.isPrototypeOf()###

    object.isPrototypeOf(o)

    var o = new Object(); // Create an object
    Object.prototype.isPrototypeOf(o) // true: o is an object
    Function.prototype.isPrototypeOf(o.toString); // true: toString is a function
    Array.prototype.isPrototypeOf([1,2,3]); // true: [1,2,3] is an array
    // Here is a way to perform a similar test
    (o.constructor == Object); // true: o was created with Object() constructor
    (o.toString.constructor == Function); // true: o.toString is a function
    // Prototype objects themselves have prototypes. The following call
    // returns true, showing that function objects inherit properties
    // from Function.prototype and also from Object.prototype.
    Object.prototype.isPrototypeOf(Function.prototype);
    

    Object.keys()###

    Object.keys(o)

    Object.keys({x:1, y:2}) // => ["x", "y"]
    

    Object.propertyIsEnumerable()###

    object.propertyIsEnumerable(propname)

    var o = new Object(); // Create an object
    o.x = 3.14; // Define a property
    o.propertyIsEnumerable("x"); // true: property x is local and enumerable
    o.propertyIsEnumerable("y"); // false: o doesn't have a property y
    o.propertyIsEnumerable("toString"); // false: toString property is inherited
    Object.prototype.propertyIsEnumerable("toString"); // false: nonenumerable
    
  • 相关阅读:
    CODING x 百果园 _ 水果零售龙头迈出 DevOps 体系建设第一步
    Nocalhost 亮相 CD Foundation 国内首届 Meetup,Keith Chan 将出席致辞
    做云原生时代标准化工具,实现高效云上研发工作流
    打造数字化软件工厂 —— 一站式 DevOps 平台全景解读
    WePack —— 助力企业渐进式 DevOps 转型
    CODING Compass —— 打造行云流水般的软件工厂
    Nocalhost —— 让云原生开发回归原始而又简单
    CODING 代码资产安全系列之 —— 构建全链路安全能力,守护代码资产安全
    Nocalhost:云原生开发新体验
    使用 Nocalhost 开发 Kubernetes 中的 APISIX Ingress Controller
  • 原文地址:https://www.cnblogs.com/jinkspeng/p/4286123.html
Copyright © 2011-2022 走看看