JS Object Properties Related--check properties, enumeration properties, etc.

Source: Internet
Author: User
Tags hasownproperty

1. Delete Attributes

The delete operator can delete an object's properties

Delete // that person no longer has attribute age Delete // or so

Delete simply disconnects the property from the host object and does not go to the property in the Action property see delete A.P after b.x still 1

var a = {p:{x:1}}; var b =//1Delete//TypeError a.p is undefined  //undefined//1

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

functioninherit (p) {if(p = =NULL){//cannot inherit from null        ThrowTypeError (); }    if(object.create) {//If you have this method, use it directly .        returnobject.create (P); }    vart =typeofp; if(t!== "Object" | | t!== "function") {//the object type to inherit is to conform to the        ThrowTypeError (); }    functionF () {};//to define an empty constructorF.prototype = p;//the prototype points to the object to inherit P    return Newf ();//creates an F object that inherits from the P}varobj = {X:1};varObj1 =inherit (obj); Obj1.y= 2; Console.log ("x =" +obj1.x+ "y =" +obj1.y);//x = 1 y = 2Deleteobj1.x;DeleteObj1.y;console.log ("x =" +obj1.x+ "y =" +obj1.y);//x = 1 y = undefined

Of course, configurable properties are used to delete
Like what

Delete // cannot remove  non-configurable var x = 1; Delete  This // Cannot delete this. y = 1; Delete // This allows you to delete function f () {} Delete  This // Cannot delete

2. Detection Properties

Use "in"

The in operator expects that its left operand is a string or can be converted to a string, and that the right operand of it is an object

vardata = [5,6,7];console.log ("0"inchdata);//There are subscript 0Console.log (1inchdata);//1 can be converted to "1"Console.log ("4"inchdata);//Subscript only 1 2 3 varobj = {X:1};console.log ("X"inchOBJ);//trueConsole.log ("Y"inchOBJ);//falseConsole.log ("ToString"inchOBJ);//true Because obj inherits this method

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

var obj = {x:1};console.log (obj.hasownproperty (///true//False  //false  because obj inherits this method, but not its own

The latter returns true only if the free attribute is detected and is an enumerable property.

var obj = {x:1};console.log (obj.propertyisenumerable (///true//  False//false  because obj inherits this method, but not its own //false because the  most primitive ToString is non-enumerable 


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

var obj = {x:1!== undefined); // trueconsole.log (obj.y!== undefined); // false // true

3. Enumeration properties

var obj = {x:1,y:2};  for inch obj) {  console.log (p); // x y  Console.log (OBJ.P); // undefined undefined  Console.log (Obj[p]); // 1 2}

Expansion 1:

Each object has a prototype (prototype), class, extensibility (extensible) associated with it

To detect whether an object is a prototype of another object (or in a prototype chain), you can use the isPrototypeOf () method

var // p prototype object inherits from Object.prototype var // o object inherits from P Console.log (p.isprototypeof (o)); // trueconsole.log (Object.prototype.isPrototypeOf (o)); // trueconsole.log (Object.prototype.isPrototypeOf (p)); // true

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

var p = {x:1instanceof Object); // true var New  instanceof Date); // true instanceof Object); // true instanceof number); // false


Expansion 2:

The class property of an object is a string that represents the type information of the object

General call to ToString () method returns form as [object class]

Like what

var obj = {x:1,y:2};console.log (obj.tostring ()); // [Object Object]

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

functionClassof (obj) {//find the class name  if(obj = = =NULL){    return"Null"; }  if(obj = = =undefined) {    return"Undefined"; }  returnObject.prototype.toString.call (obj). Slice (8,-1);} Console.log (Classof (1));// Number//Note that these types of variables are actually called the ToString () method instead of calling directly through their own//Console.log (1.toString ());//Will errorvarA = 1; Console.log (Number (1). toString ());//1Console.log (A.tostring ());//1Console.log ({x:1}.tostring ());//[Object Object]Console.log (Classof (1));// NumberConsole.log (Classof (""));//StringConsole.log (classof ("str"));//StringConsole.log (Classof (NULL));//NullConsole.log (Classof (false));//BooleanConsole.log (Classof ({}));//ObjectConsole.log (Classof ([]));//ArrayConsole.log (Classof (NewDate ()));//Datefunctionf () {}console.log (Classof (Newf ()));//Object

JS Object Properties Related--check properties, enumeration properties, etc.

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.