Methods for native JavaScript objects

Source: Internet
Author: User
Tags hasownproperty

Create

The Object.create () method creates an object that has a specified prototype and several specified properties.

Object.create(proto [, propertiesObject ])
    • Proto is the prototype object for the newly created object, set to NULL to create an empty object without a prototype.
    • Propertiesobject includes a descriptor for several properties as well as the second parameter of Defineproperties.

      Object.create (Object.prototype, {A: {value:1, Writable:true, Configurable:true}});

Create an object that inherits from Object.prototype and has an attribute a, which is writable, configurable, non-enumerable, and has a value of 1.

DefineProperty

The Object.defineproperty () method defines a new property directly on an object, or modifies an existing property and returns the object.

Object.defineProperty(obj, prop, descriptor)

The descriptor can contain 4 attributes, as follows:

    • Configurable when and only if this property descriptor value is true, the property may change or be deleted from the corresponding object. The default is False.
    • Enumerable true if and only if the property appears in the corresponding object enumeration property. The default is False.
    • The value of the Value property
    • Writable defines whether the property value is writable.
    • Get a method that provides a getter for a property, or undefined if no getter is available. method returns the value that is used as the property. The default is undefined.
    • Set is used in conjunction with GET, complementing functions.

Where value and writable are set, get and set are not allowed to appear at the same time.

// 显式Object.defineProperty(obj, "key", {enumerable: false,configurable: false,writable: false,value: "static"});

The above defines a property key for the Obj object, which is not enumerable, is not configurable, is read-only, and the value is static.

Defineproperties

The Object.defineproperties () method adds or modifies one or more of its own properties on an object and returns the object.

Object.defineProperties(obj, props)
Getprototypeof

The Object.getprototypeof () method returns the prototype of the specified object (that is, the value of the object's internal property [[[Prototype]]).

Object.getPrototypeOf(object)

Can be used to get the prototype of an object.

Object.getPrototypeOf({}) === Object.prototype// > true

Before ES5, to achieve the same method as above, you can only use constructor.

Getownpropertydescriptor

Object.getownpropertydescriptor () returns the property descriptor corresponding to a property on the specified object. (own attribute refers to a property that is directly assigned to the object and does not need to be looked up from the prototype chain)

Object.getOwnPropertyDescriptor(obj, prop)

An attribute that can be used to get or view the properties of an object.

var obj = {a: 1};Object.getOwnPropertyDescriptor(obj, ‘a‘);// > Object {value: 1, writable: true, enumerable: true, configurable: true}
({}).constructor.prototype === Object.prototype// > true

Keys

The Object.keys () method returns an array of property names for all enumerable properties of the given object, the order in which the property names are arranged in the array, and the order in which they are returned when iterating through the object using For-in (the main difference between the two is for-in It also iterates over an enumerable property that an object inherits from its prototype chain.

Object.keys(obj)

Typical usage is as follows:

var obj = {a: 1, b: 2};console.log(Object.keys(obj));// > ["a", "b"]

The keys can be used instead of the original for-in loop, which improves code readability with the addition of the ES5 array method.

Object.keys(obj).forEach(function (val) {console.log(val)});
 
Getownpropertynames

The Object.getownpropertynames () method returns an array of property names (including non-enumerable properties) of all the properties of the specified object.

Object.getOwnPropertyNames(obj)

The difference between it and Object.keys is the ability to get all of its properties, including non-enumerable properties.

Preventextensions

The Object.preventextensions () method makes an object non-extensible, that is, you can never add new properties.

Object.preventExtensions(obj)

It is important to note that properties of non-extensible objects can often still be deleted.

An attempt to add a new property to a non-extensible object will fail, be silent in non-strict mode, and throw a TypeError exception in strict mode.

Object.preventextensions can only prevent an object from adding a new self-attribute, and still add properties to the object's prototype.

Extensible objects can become non-extensible in ECMAScript 5, but not in the reverse.

Isextensible

The Object.isextensible () method determines whether an object is extensible (whether a new property can be added to it).

Seal

The Object.seal () method allows an object to be sealed and returned to the sealed object. Sealed objects are objects that cannot add new properties, cannot delete existing properties, and cannot modify the enumerable, configurable, writable, but possibly modifiable properties of an existing property.

Object.seal(obj)

Sealing an object changes the object to become non-extensible, and all existing properties become non-configurable. The effect that a property is not configurable is that the property becomes non-deleted, and that a data property cannot be redefined as an accessor property, or vice versa. However, the value of the property can still be modified.

Attempts to delete a sealed object's properties or to convert a sealed object's properties from a data property to an accessor property silently fail or throw a TypeError exception (strict mode).

IsSealed

The object.issealed () method determines whether an object is sealed (sealed).

Freeze

The Object.freeze () method can freeze an object. Frozen objects are those that cannot add new properties, cannot modify values of existing properties, cannot delete existing properties, and cannot modify the enumerable, configurable, and writable objects of an existing property. In other words, this object is always immutable. The method returns the object that was frozen.

Object.freeze(obj)

Frozen objects are non-extensible, sealed, and the writable of a period value attribute will be set to False,set will also be invalidated, in short, it will become immutable. Any attempt to modify the object will fail, either silently, or it may throw an exception (strict mode).

IsFrozen

The Object.isfrozen () method determines whether an object is frozen (frozen).

Assign

The Object.assign () method copies the enumerable properties of any number of source objects themselves to the target object, and then returns the target object.

Object.assign(target, ...sources)
Getownpropertysymbols

The Object.getownpropertysymbols () method returns an array that contains all the symbol property keys for the specified object itself (not inherited).

Is

The Object.is () method is used to determine whether two values are the same value.

The behavior of object.is and strict comparison operators (= = =) is basically the same, except that there are only two differences: one is +0 not equal to 0, and the other is Nan equals itself.

Setprototypeof

Sets the prototype of a specified object to another object or null (both [[Prototype]] internal properties of the object).

Object.setPrototypeOf(obj, prototype)
Object.prototype

The methods on the Object.prototype are instance methods that must be called on the object instance.

    • hasOwnProperty
    • Isprototypeof⑤
    • Propertyisenumerable⑤
    • __proto__⑥
hasOwnProperty

The hasOwnProperty () method is used to determine whether an object contains a specified property of its own.

obj.hasOwnProperty(prop)

Before Object.keys, with the help of hasOwnProperty, you can get a similar effect for the for in, with the following code:

for(var key in obj) {if (obj.hasOwnProperty(key)) {//过滤掉原型上的方法}}
isPrototypeOf

The isPrototypeOf () method tests whether an object exists on the prototype chain of another object.

prototype.isPrototypeOf(object)
propertyIsEnumerable

The propertyIsEnumerable () method returns a Boolean value that indicates whether the specified property name is an enumerable property of the current object.

obj.propertyIsEnumerable(prop)

The property inherited from the prototype chain, so the method returns false.

If the object does not have a specified property, the method returns false.

__proto__

The __proto__ property of an object and its own internal property [[Prototype]] point to an identical value (usually referred to as a prototype), and the value of the prototype can be either an object value or null (for example, object.prototype.__proto__ Value is null). This property may cause some errors because the user may not know the particularity of the property and assign it a value, thus altering the object's prototype. If you need to access a prototype of an object, you should use method object.getprototypeof. The __proto__ attribute has been added to the ES6 draft §b.3.1.

 

Methods for native JavaScript objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.