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
原文地址:https://www.cnblogs.com/jinkspeng/p/4286123.html