JavaScript Object Objects

Source: Internet
Author: User
Tags hasownproperty

Object 1. Introduced

Object, which is a superclass (base class) for all JavaScript objects. Defines the basic methods and properties of the JS object.

2. Constructor 2.1 New Object (): Returns an object instance 2.2 new Object (value): Returns different objects (number, Boolean, String) based on value

Parameters:

①value {Number | bool | string}: A numeric, Boolean, or string

return value:

{number, Boolean, String} returns a converted Object

Example:

var o = new Object (123); Console.log (o); = = Number Object o = new Object (true); Console.log (o); = = Boolean Object o = new Object (' abc '); Console.log (o); = = String Object

3. Instance Properties 3.1 __proto__: Sets or returns the prototype of an object

Description

1) When assigning a value, the object inherits all the methods and properties of the new prototype, as well as all methods and properties in the prototype chain of the new prototype.

2) The attribute name begins and ends with two underscores.

Example:

1. Direct object var s = {};console.log (s.__proto__); = = Object {}: The prototype defaults to object//2. Set prototype to array s.__proto__ = new Array (); Console.log (s.__proto__); = = []//3. Set a constructor var people = function (name) {    this.name = name;} s.__proto__ = new People (); Console.log (s.__proto__); = = People {name:undefined}: Prototype changed to people//4. Set a direct object var student = {Age:1};s.__proto__ = Student;console.log (s.__ PROTO__); = = Object {age:1}: prototype changed to Object

3.2 Prototype: Set or return Object classThe prototype

Description

1) This function is an attribute of the object class . __proto__ is the property of the object .

2) JavaScript internal objects have a read-only prototype property. You can add properties and methods to your prototype, but you cannot assign other prototypes to objects.

3) The prototype property of the custom object can be read and write.

Example:

var Student = function (name) {    this.name = name;};/ /Add a SayHello method to student's prototype Student.prototype.sayHello = function () {    alert (' Hello, ' + this.name);} var st = new Student (' Zhang San '); Initializes the object Stconsole.log (St.name); = Zhang San St.sayhello (); Pop up: Hello, Zhang San

4. Example Method 4.1 hasOwnProperty (PropertyName): Determines whether an object has an instance property of a specified name (not inherited)

Parameters:

①propertyname {String}: property name.

return value:

{BOOL} Determines whether the object has a locally defined (non-inherited) property of the specified name, and this method does not check the properties in the object's prototype chain.

True: property is an instance property of an object, not an inheritance.

False: property is not an instance property of an object.

Example:

1.Object object var o = new Object (); o.name = ' custom attribute '; Defines an instance attribute Console.log (O.hasownproperty (' name ')); the = = True:name attribute is defined by the instance o itself, not the inheritance Console.log (O.hasownproperty (' toString ')); = = False:tostring for inheritance properties//2. Custom object var Student = function (name) {    this.name = name;};/ /Add a SayHello method to student's prototype Student.prototype.sayHello = function () {    alert (' Hello, ' + this.name);} Add an Age attribute to Student's prototype Student.prototype.age = "; var st = new Student (' Zhang San '); Initializes the object Stconsole.log (St.hasownproperty (' name ')); = = true: Console.log (St.hasownproperty (' SayHello ')) is appended to the instance object via THIS.name when the constructor is called; = = False:sayhello method is a member of the prototype Console.log (St.hasownproperty (' age ')); = = False:age property is a member on the prototype

4.2 isprototypeof (OBEJCT): Determine if a prototype appears in the prototype chain of an object

Grammar:

Prototype.isprototypeof (object)

Parameters:

①obejct {Object}: the object being detected.

return value:

{BOOL} returns whether a prototype appears in the object's prototype chain

true: Yes

False: Not

Example:

1.Obejct object var o = new Object (); Console.log (Object.prototype.isPrototypeOf (o)); = = True:o for obejct an object//2.Arrayvar array = [1, 2, 3];console.log (Array.prototype.isPrototypeOf (array)); = = true: Array prototype Console.log (Object.prototype.isPrototypeOf (array)); = = True:object is the base prototype for all objects//3. Custom object var people = function () {}var Student = function () {}//Setting Student class prototype is Peoplestud Ent.prototype = new People (), var st = new Student (); Console.log (Student.prototype.isPrototypeOf (ST)); = = True:st for student an object Console.log (People.prototype.isPrototypeOf (ST)); The prototype of True:student is Peopleconsole.log (Object.prototype.isPrototypeOf (ST)); =>true:object is the base prototype for all objects

4.3 propertyisenumerable (PropertyName): Determines whether a property of the specified name is an instance property and is enumerable (available For/in loop enumeration)

Parameters:

①propertyname {String}: Property name

return value:

{BOOL} Determines whether the property is an instance property and is enumerable (available For/in loop enumeration), regardless of the members in the prototype chain.

true: Yes

False: Not

Example:

1.Array object var array = [1, 2, 3];array.name = ' array '; Console.log (array.propertyisenumerable (' name ')); The = = True:name property is an instance attribute Console.log (array.propertyisenumerable (' join ')); = = False:join method inherits from Arrayconsole.log (array.propertyisenumerable (' length ')); = = False:length attribute inherits from Arrayconsole.log (array.propertyisenumerable (' toString ')); = = False:tostring method inherits from object//2. Custom object var Student = function (name) {    this.name = name;} Define a prototype Method Student.prototype.sayHello = function () {    alert (' Hello ' + this.name);}; var a = new Student (' Tom '); Console.log (a.propertyisenumerable (' name ')); = = True:name The instance attribute Console.log (a.propertyisenumerable (' age ') defined for itself); = = False:age property does not exist, also returns Falseconsole.log (A.propertyisenumerable (' SayHello ')); = = False:sayhello belongs to the prototype method

4.4 tolocalestring (): Returns a localized string representation of the current object 4.5 toString (): Returns a string representation of the current object 4.6 valueOf (): Returns the original value of the current object

Parameters: None

return value:

{Object} returns the original value associated with the current object and, if there is no associated value, returns the object itself

Example:

var a = [1, 2, 3];console.log (a.valueof ()); = = [1, 2, 3]var B = True;console.log (b.valueof ()); = = Truevar c = {};console.log (c.valueof ()); = = Object {}var s = ' abc '; Console.log (s.valueof ()); + = abc//Custom object, overriding Valueofvar CustomObject = {};customobject.valueof = function () {    return ' custom object ';} Console.log (Customobject.valueof ()); + = Custom Objects

5. Static method 5.1 Object.create (prototype, PropertyDescriptor): Creates and returns an object that specifies a prototype and a specified property

Parameters:

①prototype {prototype}: Returns the prototype of the object, which can be null.

②propertydescriptor {PropertyDescriptor} Optional: Property descriptor.

Property Descriptor: Sets the properties of a series of attributes;

Syntax format:

PropertyName: {    value: ',//sets the value of this property    Writable:true,//sets whether this property is writable; default is false: Read    -only enumerable:true,// Sets whether this property can be enumerated (prepaid via For/in), false by default: Non-enumerable    configurable:true//Sets whether this property can be configured, such as: whether properties can be modified and delete properties. The default is False}

return value:

{Object} returns an object that specifies a prototype and a specified property

Example:

Create a custom object, set the name and age property var obj = Object.create (null, {    name: {        value: ' Tom ',        Writable:true,        Enumerable:true,        configurable:true    }, age    : {        value:22    }}); Console.log (obj.name);//= = Tomconsole.log (Obj.age); = = 22obj.age = 28;console.log (obj.age); The writable default for the 22:age property is False, this property is read-only for (P-in obj) {    console.log (p);//= = Name: Only the Name property is output; The age property of the enumerable Considered false, cannot be enumerated by for/in}

5.2 Object.defineproperties (object, PropertyDescriptor): Add/Modify attributes of one or more properties of an object

Parameters:

①object {Object}: Object

②propertydescriptor {PropertyDescriptor} property descriptor.

Description

If the object contains this property, the attribute is modified, otherwise this property is added for the object.

Example:

var obj = {};//Adds the name and age property to the object Object.defineproperties (obj, {    name: {        value: ' Tom ',        enumerable:true    } , age    : {        value:22,        enumerable:true     }}); for (p in obj) {    console.log (p);//= = name, Age: Output Name and a GE Properties}

  

5.3 Object.defineproperty (obj, PropertyName, PropertyDescriptor): adding/Modifying attributes of an object's specified properties

Parameters:

①object {Object}: Object

②propertyname {string}: Name of the property

③propertydescriptor {PropertyDescriptor} property descriptor.

Description

If the object contains this property, it is the attribute that modifies the property; otherwise, this property is added.

Example:

var obj = {};//Adds a name attribute object.defineproperty (obj, ' name ', {        value: ' Tom ',        Writable:true,        Enumerable: True,        configurable:true    }); Console.log (Obj.name); = Tom: Value of the output Name property

5.4 Object.freeze (object): Freezes an object so that it cannot add properties and cannot make attribute changes, value modifications, property deletions, and so on for existing instance properties

Parameters:

①object {Object}: Object

Description

1) This operation is irreversible and cannot be unpacked after freezing.

2) only affects instance properties, and does not affect prototype properties.

Example:

var obj = {    name: ' Tom ',    Age:22};object.freeze (obj);//Freeze Object obj.email = ' [email protected] '; Console.log ( Obj.email); Undefined: Could not add attribute Obj.age = 25;console.log (obj.age); 22: Unable to set the value of the property

5.5 Object.getownpropertydescriptor (Object, PropertyName): A descriptor that returns the properties of an object

Parameters:

①object {Object}: Object

②propertyname {PropertyName} property name

return value:

{PropertyDescriptor} Property descriptor Object

Example:

var obj = {    name: ' Tom ',    age:22};var propertydes = object.getownpropertydescriptor (obj, ' name '); Console.log ( Propertydes); = = Object {value: "Tom", Writable:true, Enumerable:true, configurable:true}: Output Descriptor objects

  

5.6 Object.getownpropertynames (Object): Returns an array that contains all instance properties and methods of the object, without the properties and methods of the stereotype inheritance

Parameters:

①object {Object}: Object

return value:

{array} An array containing all instance properties and methods of the object, without the properties and methods of the stereotype inheritance

Example:

var obj = {    name: ' Tom ',    age:22,    sayhello:function () {        alert (' Hello ' + this.name);    }}; Console.log (Object.getownpropertynames (obj)); = = ["Name", "Age", "SayHello"]: Returns an instance member of an object

5.7 Object.getprototypeof (object): Returns the upper-level prototype of an object

Parameters:

①object {Object}: Object

return value:

{Object} returns prototype Object

Example:

1. Object Direct amount var obj = {    name: ' Tom ',    age:22,    sayhello:function () {        alert (' Hello ' + this.name);    }}; Console.log (object.getprototypeof (obj)); = = Object object: The prototype of the direct amount of objects is object//2. Custom object var people = function (name) {    this.name = name;}; var p = new People (' Tom '), var people = object.getprototypeof (p); Console.log (people); = = People object: The object created by new uses the prototype property of the constructor as their prototype Console.log (Object.getprototypeof (people)); = = object: Prototype of prototype people is object

  

5.8 Object.isextensible (object): Determines whether a new property can be added to the object 5.9 Object.isfrozen (object): Determines whether the object freezes; True: Cannot modify an object's existing property attributes and values and cannot add a new property 5.10 object.issealed (object): Determines whether an object is closed; True: Cannot modify an existing property attribute of an object and cannot add a new property

5.11 Object.keys (object): Returns an array that contains the names of the enumerable properties and methods of the object

Parameters:

①object {Object}: Object

return value:

{array} returns an array that contains the names of the enumerable properties and methods of the object

Description

This method is similar to Getownpropertynames (), but Getownpropertynames () contains enumerable and non-enumerable members

Example:

var obj = {    name: ' Tom ',    age:22,    sayhello:function () {        alert (' Hello ' + this.name);    }};/ /1) The content returned by the Getownpropertynames and the keys method is the same console.log (Object.getownpropertynames (obj)); = = ["Name", "Age", "SayHello"]: Returns all instance members of the object Console.log (Object.keys (obj)); = = ["Name", "Age", "SayHello"]: Returns all enumerable members of an object//Settings object's Name property is not enumerable object.defineproperty (obj, ' name ', {        Enumerable:false    });//2) The keys method, which contains only enumerable member Console.log (Object.getownpropertynames (obj)); = = ["Name", "Age", "SayHello"]: Returns all instance members of the object Console.log (Object.keys (obj)); = = ["Age", "SayHello"]: Returns all enumerable members of an object

5.12 Object.preventextensions (object): Setting object cannot add new properties

Parameters:

①object {Object}: Object

return value:

{Object} returns this object

Example:

var obj = {    name: ' Tom ',    age:22};object.preventextensions (obj);//Set object Cannot add new property Obj.email = ' [email protected] '; Console.log (Obj.email); = = undefined: Cannot add new property to Object

5.13 Object.seal (object): Seals an object so that it cannot modify the attributes of an existing property and cannot add a new property

Parameters:

①object {Object}: Object

return value:

{Object} returns this object

Example:

var obj = {    name: ' Tom ',    age:22};object.seal (obj);//Sealed object obj.email = ' [email protected] '; Console.log (obj.email ); = = undefined: Unable to add new attribute//Report exception to object: Cannot modify attribute Object.defineproperty (obj, ' name ', {        enumerable:false}) of object property    ;

6. Property descriptors

Divided into data attributes and accessor attributes;

The two can be converted to each other, and if the enumerable and configurable attributes are not set after the conversion (the two Class property descriptor contains these 2 attributes), the pre-conversion value will be used by default.

6.1 Data Properties

Description: An action attribute that contains a property, such as setting a value, enumerable, and so on

Attribute name Describe Default value
Value Set the value of a property Undefined
Writable Whether the value of the property can be modified; true: The value of the property can be modified; false: The value of a non-modifiable property False
Enumerable Whether attributes can be enumerated, true: enumerable, properties can be enumerated through for/in statements, false: non-enumerable False
Configurable Whether the attributes of the property can be modified, true: properties of the property can be modified (such as changing writable from false to True); false: attributes of non-modifiable properties False

Default value:

1) Add data properties with the Object.defineproperty, object.defineproperties, or object.create functions, writable, The default value for enumerable and configurable is false.

2) Properties created using the object's direct volume, writable, enumerable, and configurable attributes default to True.

Example:

1) object Direct volume; property attribute defaults to Truevar O1 = {    name: ' Tom '};console.log (Object.getownpropertydescriptor (O1, ' name '));//= = Object {value: "Tom", Writable:true, Enumerable:true, configurable:true}//2) created by Object.create, property attribute defaults to Falsevar O2 = OBJ Ect.create (null, {    name: {value: ' Tom '}}); Console.log (Object.getownpropertydescriptor (O2, ' name '));//= = Object {value: "Tom", Writable:false, Enumerable:false, Configurable:false}

6.2 Accessor Properties

Description: Set the access mode of the property, set, get properties, etc.

Attribute name Describe Default value
Get Property's return value function Undefined
Set The set-value function of the property, which contains an assignment parameter Undefined
Enumerable Whether attributes can be enumerated, true: enumerable, properties can be enumerated through for/in statements, false: non-enumerable False
Configurable Whether the attributes of the property can be modified, true: properties of the property can be modified (such as changing writable from false to True); false: attributes of non-modifiable properties False

Example:

var obj = {};//Adds a property and is set to accessor property Object.defineproperty (obj, "name", {    get:function () {        return this._name;//Get and The variables in set do not use attributes, such as: Name,get and set with _name    },    set:function (x) {        if (IsNaN (x)) {            this._name = x;        } else {            This._name = ' name cannot be a pure number ';        }    },    enumerable:true,    configurable:true}); Console.log ( Object.getownpropertydescriptor (obj, ' name ')); = = Object {get:function, set:function, Enumerable:true, configurable:true} obj.name = ' n '; Console.log (Obj.name) ; = = Name cannot be a purely numeric

================================== Series article ==========================================

This article: 3.7 JavaScript Object objects

Web Development Road Series articles

JavaScript Object 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.