How to use objects in javascript _ javascript skills

Source: Internet
Author: User
Tags hasownproperty
This article describes how to use objects in javascript in a comprehensive and detailed manner. For more information, see The code is as follows:


Function forEach (o ){
Var html = "";
For (var I in o ){
Html + = I + "=" + o [I] + "";
}
Console. log (html );
Console. log (o );
}

// 1
// Object. create (proto)
// Object. create (proto, descriptors)
// Create an object using the specified prototype and attributes
// Parameters:
// Proto: the prototype of the newly created object. it can be null.
// Descriptors: an optional object that maps attribute names to attribute descriptors.
// Return a newly created object that inherits from proto and has the attributes of descriptors in seconds.

The code is as follows:


Var obj = Object. create ({x: 1, y: 2 },{
Z: {value: 3, writable: true, enumerable: true, retriable: true}
});
ForEach (obj)
Obj. z = 5
Console. log (obj)
Console. log ("============================================ ==================== ")

// 2
// Object. defineProperties (o, descriptors)
// Create or configure multiple attributes of an object
// Parameters:
// O: The object on which you want to create or configure attributes
// Descriptors: the object that maps attribute names to attribute descriptors.
// Returns the object o

The code is as follows:


Object. defineProperties (obj ,{
A: {value: "a", writable: false, enumerable: true, retriable: true },
B: {value: "B", writable: false, enumerable: true, retriable: true}
})
ForEach (obj );
Console. log ("============================================ ==================== ")

// 3
// Object. defineProperty (o, name, desc)
// Create or configure an attribute of an object
// Parameters:
// O: The object on which the property will be created or configured
// Name: name of the property to be created or configured
// Desc: an attribute descriptor object that describes the new attribute to be created or the modification to the existing attribute.
// Returns the object o

The code is as follows:


Object. defineProperty (obj, "c", {value: "c", writable: false, enumerable: false, retriable: true })
ForEach (obj );
Console. log ("============================================ ==================== ")

// 4
// Object. freeze (o)
// Setting an object as unchangeable does not affect the inheritance attributes
// Parameters:
// O: object to be frozen
// Returns true | false

The code is as follows:


Var p = {x: 1, y: 2}
Object. freeze (p );
P. x = 2;
Console. log (p );
Console. log (Object. isFrozen (p) // true, unfrozen once frozen
Console. log ("============================================ ==================== ")

// 5
// Object. getOwnPropertyDescriptor (o, name)
// Parameters:
// O: an object
// Name: name of the property to be queried
// Query the features of an attribute
// Return an attribute descriptor object of a specified property of the object. If no specified property exists, undefined is returned.
/*
Attribute descriptor is a common javascript object that describes the characteristics of an object. It has two types of javascript attributes. Data attributes have one value and three properties: enumerable ),
Writable and configurable. the accesssor property has a getter and/or setter method and can be enumerated.
Data attribute descriptor:
{
Value: Any javascript value,
Writable: true | false,
Enumerable: true | false,
Retriable: true | false
}
Descriptor of the accessors attribute:
{
Get: function or undefined: replace the attribute value
Set: function or undefined: replace writability
Enumerable: true | false,
Retriable: true | false
}
*/

The code is as follows:


Var o5 = Object. getOwnPropertyDescriptor (obj, "c ");
Console. log (o5 );
ForEach (o5 );
Console. log ("============================================ ==================== ")

// 6
// Object. getOwnPropertyNames (o)
// Return the name of a non-inherited attribute
// Parameters:
// O: an object
// Return the names of all non-inherited properties containing o, including non-enumerated properties. {Enumerable: false}

The code is as follows:


Var o6 = Object. getOwnPropertyNames (obj );
Console. log (o6 );
Console. log ("============================================ ==================== ")

// 7
// Object. getPrototypeOf (o)
// Parameters:
// O: an object
// Return the prototype of an object

The code is as follows:


Var o7 = Object. getPrototypeOf (obj );
Console. log (o7 );
Console. log ("============================================ ==================== ")

// 8
// Object. hasOwnProperty (propname );
// Parameters:
// A string whose propname contains the object's property name
// Check whether an attribute is inherited
// Returns true | false

The code is as follows:


Console. log (obj. hasOwnProperty ("x"); // => false
Console. log (obj. hasOwnProperty ("z"); // => true
Console. log ("============================================ ==================== ")

// 9
// Object. isExtensible (o );
// Determine whether a new attribute can be added to an object
// Parameters:
// O: The object to be checked for extensibility
// The return value can be added to true | the return value cannot be false.
// Description: all objects can be extended during creation until they are passed in Object. preventExtensions (o) Object. seal (o) or Object. freeze (o );

The code is as follows:


Console. log (Object. isExtensible (obj); // => true
// Object. preventExtensions (obj) // Set it to unextensible
// Console. log (Object. isExtensible (obj); // => false
Console. log ("============================================ ==================== ")

// 10
// Object. isFrozen (o)
// Determine whether the object cannot be changed
// Parameters:
// O: object to be checked
// True if the o has been frozen and does not change; otherwise, false;

The code is as follows:


Console. log ("============================================ ==================== ")

// 11
// Object. isPrototypeOf (o)
// Determine whether the current object is a prototype of another object
// Parameters:
// O: all objects
// True if the object is an o prototype. false if o is not an object or the object is not an o prototype.

The code is as follows:


Var o = new Object ();
Object. prototype. isPrototypeOf (o) // true
Array. prototype. isPrototypeOf ([1, 2]) // true;
Object. prototype. isPrototypeOf (Function. prototype) // true
Console. log ("============================================ ==================== ")

// 12
// Object. isSealed (o)
// Determine whether an object's attributes can be added or deleted
// Parameters:
// O: object to be checked
// True if o is closed; otherwise, false.
// If you cannot add a new (non-inherited) attribute to an object and the existing (non-inherited) attribute cannot be deleted, it is closed.
// The common method to close an Object is Object. seal (o) or Object. freeze (o)

Console. log ("============================================ ==================== ")

// 13
// Object. keys (o)
// Return the free enumerated property name
// Parameters:
// O: an object

The code is as follows:


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

// 14
// Object. preventExtensions (o)
// Do not add new attributes to an object
// Parameters:
// O: The object to be expanded
// Once it is set to unextensible, it cannot be changed to extensible.

Console. log ("============================================ ==================== ")


// 15
// Object. propertyIsEnumerable (propname)
// Checks whether a property is cyclically visible in for/in.
// Parameters
// Propname: a string containing the specified property name of the object
// If the object has a non-inherited property named propname that can be enumerated, true is returned.

The code is as follows:


Var o15 = new Object ();
O15.x = 15;
O15.propertyIsEnumerable ("x"); // true;
O15.propertyIsEnumerable ("y"); // false;
O15.propertyIsEnumerable ("toString"); // false;
Console. log ("============================================ ==================== ")

// 16
// Object. seal (o)
// Block adding or deleting attributes of an object
// Parameters
// O: The object to be closed
// Return the closed Parameter object o

// 17
// Object. toLocaleString ()
// Returns the local string of the object.
// The default toLocaleString () method provided by the Object class is simply to call the toString () method.
// Note that other classes (such as Array, Date, and Number) define their own versions of this method. It is used to perform localized string conversion. You may need to override this method when defining your own class.

// 18
// Object. toString ()
// Define the string representation of an object
// The toString () method is usually not displayed in javascript programs. Generally, when this method is defined in an object, the system automatically calls it as needed to replace the object with a string.

// 19
// Object. valueOf ()
// Original value of the given object
// Return the original value associated with the specified object. if such a value exists, if the value is not associated with the modified object, the object itself is returned.

Related Article

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.