js object

Object is the basic data type of js. An object is a composite value: it aggregates together many values ​​(primitive or other objects), which can be accessed by name. Objects can also be thought of as unordered collections of properties, each of which is a name/value pair.
(1) Create an object
var point = {a:1,b:2};
var a = new Object();
var o = Object.create({a:1,b:2});
Object.create() is A static function, not a method provided to be called on an object. The method to use it is to pass in the desired prototype object.
(2) Attribute query and setting
You can obtain the attribute value through the dot (.) or square bracket ([]) operator. The left side of the operator should be an expression, which returns an object. For a dot (.), the right-hand side must be a simple identifier named after the property name. For square brackets ([]), the brackets must be an expression that evaluates to a string, which is the name of the property.
var counts = list.counts; //Get the counts property of the list
var barcode = inputs[barcode]; //Get the barcode property of the inputs
(3) Delete the property
The delete operator can delete the property of the object. Its operand should be an attribute access expression. delete just disconnects the property from the host, but does not operate on the properties in the property. Properties whose configurability is false cannot be removed.
delete list.counts; //Delete the counts property of the list
delete inputs[barcode]; //Delete the barcode property of the inputs
(4) Detecting properties
can be done through the in operator, hasOwnPreperty() and propertyIsEnumerable() methods.
var list = {counts: 1}
"counts" in list; //true:"counts" is a property of list
list.hasOwnPreperty("counts"); //true:list has its own property counts
list.propertyIsEnumerable(" counts") //true:list has its own attribute counts
(5) Traversal query
You can use for/in to traverse
var list = {a:1,b:2,c:3};
for(p in list)
console .log(p) //results are a,b,c
console.log(list[p]) //results are 1,2,3

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326308847&siteId=291194637