Everything in JavaScript is an object: strings, numbers, arrays, functions… In addition, JavaScript allows you to customize objects. I hope you found this article helpful

Create an object

Object direct quantity An object direct quantity is a mapping table consisting of several name/value pairs separated by colons, separated by commas, and enclosed in curly braces.

var empty = {}; var point = { x:0, y:0}; var point2 = {x:point.x, y:point.y+1}; Var book = {"main title":"JavaScript", "For ":"all", "for":"all", "for":"all", "for":"all", "for":"all", "for":" All"Copy the code

Create objects with new

var o = new Object(); Var a = new Array(); // Create an empty array like [] var d = new Date(); // Create a Date object representing the current time var r = new RegExp("js"); // Create a matching RegExp objectCopy the code

Prototype All objects created directly from objects have the same prototype Object and can be referenced by the JavaScript code object.prototype. The prototype of the object created through the keyword new and constructor calls is the value of the constructor’s Prototype property. Therefore, objects created by new Object () inherit from Object.prototype just like objects created with {}. Similarly, the prototype of an object created by new Array () is array.prototype, and the prototype of an object created by new Date () is date.prototype.

Object. Prototype is one of the few objects without a prototype. It does not inherit any attributes. All other stereotype objects are ordinary objects, and ordinary objects have stereotypes. All built-in constructors (and most custom constructors) have a prototype inherited from Object.prototype. For example, the properties of date.prototype inherit from Object.prototype, so the properties of a Date Object created by new Date () inherit from both Date.prototype and Object.prototype. This series of linked prototype objects is known as the prototype chain.

Object.create () creates a new Object, where the first argument is the prototype of the Object. Object.create () provides a second optional argument that further describes the attributes of the Object.

Object.create () is a static function, not a method provided to be called by an Object. Using it is as simple as passing in the desired prototype object:

var AB = Object.create({x:1,y:2});
Copy the code

Properties to query and set

var a = book.author; // Get the "author" attribute book. Edition = 6; // Create an attribute for book named "edition" and assign book["main title"] = "123" // assign the "main title" attribute to bookCopy the code

Inheritance assumes that property X of object O is to be queried. If x does not exist in O, property X will continue to be queried in o’s prototype object. If there is no x in the prototype object, but the prototype object also has a prototype, the query continues on the prototype of the prototype object until x is found or an object with a null prototype is found.

var o = {} o.x = 1; X var p = inherit(o); //p inherits o p y = 2; Y var q = inherit(p); P q.z = 3; X + q.y // 3 x and y inherit from o and p, respectivelyCopy the code

The attribute assignment operation first checks the stereotype chain to determine whether the assignment operation is allowed. If attribute assignment is allowed, it will always create attributes on the original object or assign values to existing attributes without modifying the stereotype chain.

var u = { r:1 }; var c = inherit(u); c.x = 1; c.y =1; c.r =2; u.r; // 1 prototype object is not modifiedCopy the code

Property access error when book does not have a property

book.a // undefined var l = book.a.length; // Throws a type error exception, undefined has no attributeCopy the code

Delete attributes The DELETE operator deletes attributes of an object.

delete book.author; //book no longer has an attribute author delete book["main title"]; // Book no longer has property "main title"Copy the code

The delete operator can only delete an owned property, not an inherited property (to delete an inherited property you must delete it from the stereotype that defines it, and this affects all objects that inherit from the stereotype).

It returns true when the DELETE expression is deleted successfully or without any side effects (such as deleting a nonexistent property). If delete is not an attribute access expression, delete also returns true:

o = {x:1}; delete o.x; // Delete x, return true delete o.x; // Do nothing (x no longer exists), return true delete o.tostring; // Do nothing (toString is inherited), return true delete 1; // meaningless, returns trueCopy the code

Delete cannot delete properties whose configurability is false. Delete in these cases returns false:

delete Object.prototype; Var x = 1; // Declare a global variable delete this.x; Function f (){}// declare a global function delete this.f; // Global functions cannot be deleted eitherCopy the code

The detection attribute in operator has the attribute name (string) on the left and the object on the right. Returns true if the object contains this property in its own or inherited property:

var o = { x:1 } "x" in o; //true "x" is an attribute of o "y" in o; //false "y" is not an attribute of o "toString" in o; //true o inherits the toString attributeCopy the code

The object’s hasOwnProperty () method is used to check whether the given name is an object’s own property. For inherited attributes it will return false:

var o = { x:1 } o.hasOwnProperty("x"); //true o has a property of its own x. //false o does not have property y o.hasownProperty ("toString"); // False toString is an inherited propertyCopy the code

PropertyIsEnumerable () is an enhanced version of hasOwnProperty (), which returns true only if it has its own property and the enumerable attribute is true.

var o = inherit({ y:2}); o.x = 1; o.propertyIsEnumerable("x"); // True o has an enumerable property x. Pertyisenumerable ("y"); / / false y was inherited Object. PropertyIsEnumerable (" toString "); //false cannot be enumeratedCopy the code

A simpler alternative to using the in operator is to use the “! Check whether an attribute is undefined:

var o = { x:1 } o.x ! == undefined; //true o has the attribute x. Y! == undefined; //false o has no property yo.tostring! == undefined; //true o inherits the toString attributeCopy the code

Serialization Object serialization refers to converting the state of an object to a string, or restoring a string to an object. ECMAScript 5 provides built-in functions json.stringify () and json.parse () to serialize and restore JavaScript objects. Each of these methods uses JSON, which stands for “JavaScript Object Notation,” as the data interchange format.

o = {x:1, y:{z:[false,null,""]}}; s = JSON.stringify(o); / / s is' {" x ": 1," y ": {" z" : [false, null, ""]}} 'p = JSON. Parse (s); // p == oCopy the code

www.jb51.net/article/219…