Javascript Object property-related (checking attributes, enumeration attributes, etc.) _ javascript skills

Source: Internet
Author: User
Tags hasownproperty
This article mainly introduces JS object attributes (check attributes, enumeration attributes, etc.). For more information, see 1. delete attributes

The delete operator can delete object attributes.

The code is as follows:


Delete person. age // that is, the person no longer has an attribute age.
Delete person ['age'] // or

Delete only disconnects the property from the host object, and does not operate the property. after delete a. p, B. x is still 1.

var a = {p:{x:1}};var b = a.p;console.log(a.p.x); //1delete a.p;console.log(a.p.x); //TypeError a.p is undefinedconsole.log(a.p); //undefinedconsole.log(b.x); //1

Delete can only delete its own attributes, but cannot delete the inherited attributes. (to delete the inherited attributes, you must delete them from the prototype object that defines this attribute. of course, this will affect all objects inherited from this prototype)

Function inherit (p) {if (p = null) {// cannot inherit throw TypeError () from null;} if (Object. create) {// use return Object directly if this method is available. create (p);} var t = typeof p; if (t! = "Object" | t! = "Function") {// The object type to be inherited must comply with throw TypeError ();} function f () {}; // defines an empty constructor f. prototype = p; // prototype indicates the object to be inherited, p return new f (); // creates an f object, which is inherited 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, you can use delete only for configurable attributes.
For example

Delete Object. prototype; // it cannot be deleted and cannot be configured var x = 1; delete this. x; // you cannot delete this. y = 1; delete y; // you can delete function f () {} delete this. f; // cannot be deleted

2. check attributes

Use "in"

The in operator wants its left operand to be a string or can be converted to a string. it wants its right operand to be an object.

Var data = [5, 6, 7]; console. log ("0" in data); // There is a subscript 0console. 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); // trueconsole. log ("y" in obj); // falseconsole. log ("toString" in obj); // true because obj inherits this method

Use hasOwnProperty () or propertyIsEnumerable () --- the latter is the enhancement of the former.
Gu Mingyu

Var obj = {x: 1}; console. log (obj. hasOwnProperty ("x"); // trueconsole. log (obj. hasOwnProperty ("y"); // falseconsole. log (obj. hasOwnProperty ("toString"); // false because obj inherits this method, but not its own

The latter returns true only when it detects a free attribute and an enumerable attribute.

Var obj = {x: 1}; console. log (obj. propertyIsEnumerable ("x"); // trueconsole. log (obj. propertyIsEnumerable ("y"); // falseconsole. log (obj. propertyIsEnumerable ("toString"); // false because obj inherits this method, but not its own console. log (Object. prototype. propertyIsEnumerable ("toString"); // false because the original toString cannot be enumerated.

Of course, you can also use "! = "Operator judgment

var obj = {x:1};console.log(obj.x !== undefined);//trueconsole.log(obj.y !== undefined);//falseconsole.log(obj.toString !== undefined); //true 

3. enumeration properties

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} 

Expansion 1:

Each object has prototype, class, extensible)
To check whether an object is a prototype of another object (or is in the prototype chain), you can use the isPrototypeOf () method.

Var p = {x: 1}; // The p prototype Object inherits from the Object. prototypevar o = Object. create (p); // o object inherited from pconsole. 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 wants its left operand to be an object, and the right operand to identify the class of the object. If the object on the left is an instance of the right class, the expression returns true; otherwise, false.

var p = {x:1}; console.log(p instanceof Object);//truevar d = new Date();console.log(d instanceof Date);//trueconsole.log(d instanceof Object);//trueconsole.log(d instanceof Number);//false

Expansion 2:

The class attribute of an object is a string used to indicate the object type information.

Generally, after the toString () method is called, the returned result is in the form of [object class ].

For example

The code is as follows:


Var obj = {x: 1, y: 2 };
Console. log (obj. toString (); // [object Object]

Therefore, to obtain the class of the object, you can find the "class" field in the returned string and use slice (8,-1)
For example

Function classOf (obj) {// find the class name if (obj = null) {return "Null";} if (obj = undefined) {return "Undefined";} return Object. prototype. toString. call (obj ). slice (8,-1);} console. log (classOf (1); // Number // note that these types of variables call the toString () method instead of directly calling the/console. log (1. toString (); // error var a = 1; console. log (Number (1 ). toString (); // 1console. log (. 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 (new Date (); // Datefunction f () {} console. log (classOf (new f (); // Object

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.