JS Object Attributes (check properties, enumeration properties, and so on)

Source: Internet
Author: User
Tags object object tostring hasownproperty

1. Delete Attributes

The delete operator can delete an object's properties

The

code is as follows:


Delete person.age//That person no longer has attribute age


Delete person[' age ']//or so

Delete simply disconnects the property from the host object and does not go to the properties in the Action property to see the delete a.p after the b.x is still 1

1 2 3 4 5 6 7 var a = {p:{x:1}}; var B = A.P; Console.log (a.p.x); 1 Delete a.p; Console.log (a.p.x); TypeError a.p is undefined console.log (A.P); Undefined Console.log (b.x); 1

Delete can only delete its own properties and cannot delete inherited properties (to remove an inherited property you must remove it from the prototype object that defines the property, which, of course, affects all objects that inherit from this prototype)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22-23 function inherit (p) {if (p = = null) {//cannot inherit from null throw TypeError ();} if (object.create) {////If this method is used directly to return Object.cre Ate (p); var t = typeof P; if (t!== "Object" | | | | | t!== "function") {//The object type to inherit should conform to throw TypeError ();} function f () {}; Defines an empty constructor f.prototype = p; The prototype points to the object to inherit P return new F (); Creates an F object that inherits from P} var obj = {X:1}; var obj1 = inherit (obj); OBJ1.Y = 2; Console.log ("x =" +obj1.x+ "y =" +obj1.y); x = 1 y = 2 delete obj1.x; Delete obj1.y; Console.log ("x =" +obj1.x+ "y =" +obj1.y); x = 1 y = undefined

Of course, configurable properties can be used to delete
Like what

1 2 3 4 5 6 7 8 9 Delete Object.prototype; Cannot delete non-configurable var x = 1; Delete this.x; Cannot delete this.y = 1; Delete y; This can remove the function f () {} delete this.f; Cannot delete

2. Detection Properties

Using "in"

The in operator wants its left operand to be a string or it can be converted to a string, hoping that its right-hand operand is an object

1 2 3 4 5 6 7 8 9 var data = [5,6,7]; Console.log ("0" in data); With subscript 0 Console.log (1 in data); 1 can be converted to "1" Console.log ("4" in data); Subscript only 1 2 3 var obj = {X:1}; Console.log ("x" in obj); True Console.log ("Y" in obj); False Console.log ("toString" in obj); True because obj inherits this method

Using hasOwnProperty () or propertyisenumerable ()---the latter is the enhancement of the former
Discussion on Gu Mingsi

1 2 3 4 var obj = {X:1}; Console.log (Obj.hasownproperty ("X")); True Console.log (Obj.hasownproperty ("Y")); False Console.log (Obj.hasownproperty ("toString")); False because obj inherits this method, but not its own

The latter returns true only if it detects a free property and is an enumerable property

1 2 3 4 5 var obj = {X:1}; Console.log (Obj.propertyisenumerable ("X")); True Console.log (obj.propertyisenumerable ("Y")); False Console.log (obj.propertyisenumerable ("toString")); False because obj inherits this method, but not its own console.log (Object.prototype.propertyIsEnumerable ("toString")); False because the most original ToString is not enumerable

Of course, you can also directly use the "!==" operator to determine

1 2 3 4 var obj = {X:1}; Console.log (obj.x!== undefined)//true console.log (obj.y!== undefined);//false console.log (obj.tostring!== undefined); True

3. Enumeration properties

1 2 3 4 5 6 var obj = {X:1,y:2}; For (p in obj) {console.log (P);//x y Console.log (OBJ.P);//undefined undefined console.log (obj[p));//1 2}

Expand 1:

Each object has a prototype associated with it (prototype), Class (Class), Extensibility (extensible)
To detect whether an object is a prototype of another object (or in a prototype chain), you can use the isPrototypeOf () method

1 2 3 4 5 6 var p = {X:1}; P prototype object inherits from Object.prototype var o = object.create (p); O object inherits from P Console.log (p.isprototypeof (o));//true Console.log (Object.prototype.isPrototypeOf (o));//true Console.log (Object.prototype.isPrototypeOf (P));//true

Of course, the isprototypeof () method and the instanceof operator are very similar
The instanceof operator expects its left operand to be an object, and the right operand identifies the class of the object. If the object on the left is an instance of the right class, the expression returns true, otherwise it returns false

1 2 3 4 5 6 7 var p = {X:1}; Console.log (P instanceof Object);//true var d = new Date (); Console.log (d instanceof Date);//true Console.log (d instanceof Object);//true Console.log (d instanceof number);//false

Expand 2:

object's Class property is a string that represents the type information of the object

A general call to the ToString () method returns the form as [object class]

Like what

The

code is as follows:


var obj = {X:1,y:2};


Console.log (obj.tostring ());//[object Object]

So to get the object's class, you can find the "class" field in the returned string using slice (8,-1)
Like what

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 function classof (obj) {//Find the class name if (obj = = null) {return "Nu ll "; } if (obj = = undefined) {return ' undefined ';} return Object.prototype.toString.call (obj). Slice (8,-1); }   Console.log (Classof (1)); Number//note, in effect, these types of variables call the ToString () method instead of directly calling//console.log (1.toString ()) via their own; Will error the var a = 1; Console.log (number (1). ToString ()); 1 Console.log (a.tostring ()); 1 Console.log ({x:1}.tostring ()); [Object Object]   Console.log (classof (1)); Number Console.log (Classof ("")); String Console.log (classof ("str")); String Console.log (classof (null)); Null Console.log (Classof (false)); Boolean Console.log (Classof ({})); Object Console.log (Classof ([])); Array Console.log (Classof (New Date ()); Date function f () {} console.log (Classof (new F ())); Object
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.