introduce

In JavaScript, almost “everything” is an object, meaning everything is an object. All JavaScript is an object except the original value.

  • Booleans are objects (new definition)
  • Numbers are objects (new definition)
  • Strings are objects (new definition)
  • Date is object is object
  • Regular expressions are objects
  • Arithmetic is the object
  • Arrays are objects
  • Functions are objects
  • An object is always an object
JavaScript original value
  • string
  • number
  • boolean
  • null
  • undefined
An object is a variable that contains variables

Objects are also variables. But objects can contain many values. Format: key (Name): value

Var obj = {name: 'zhang3 ', age: 18}Copy the code

Named values (keys, names) are called attributes

Create an object

Literal creation

With object literals, you can define and create objects in a single statement. Object literals refer to names in curly braces {} : value pairs.

Var obj = {name: 'c ', age: 18} console.log(typeof obj) // objectCopy the code
Keywords New create
Var obj = new Object() obj = new Object() obj = new Object() obj.Copy the code

Objects are mutable

Var obj = {name: 'console.log ', age: 18} var b = obj b.name =' console.log '(b.name) // obj console.log(obj.name) // objCopy the code

Solutions:

  • Object: the assign
  • Array: slice
  • use.Assign a new variable to an array object
  • JSON.parse(JSON.stringify(obj))
Var obj = {name: 'age ', age: 18} var b = object.assign ({}, obj) b.name = 'li' console.log(b.name) console.log(obj.name)Copy the code

attribute

Properties refer to values associated with JavaScript objects. It can be modified, deleted, or added.

To access attributes
Var obj = {name: ' ', age: 18} // 2, obj["property"] obj['name']Copy the code
Iterate over the properties of an object

for… In traverses object properties.

Var obj = {name: 'obj ', age: 18} for (var key in obj) {console.log(key, obj])}Copy the code

Add attributes
Var obj = {name: '3 ', age: 18} obj. Sex =' male 'obj['phone'] = '125245' console.log(obj) // {name:' 3 ', age: 18, sex: "Male ", phone: "125245"}Copy the code
Delete the properties

Delete Deletes an object attribute. The delete keyword does not delete inherited properties, but if you delete a stereotype property, it affects all objects inherited from the stereotype.

Var obj = {name: 'c ', age: 18} delete obj.name console.log(obj) // {age: 18}Copy the code

Object methods

JavaScript methods are actions that can be performed on objects.

thiskeywords

This refers to the object that owns the JavaScript code. When used within a function, this is the object that “owns” the function. This is a keyword, not a variable, and you cannot change the value of this.

Access object methods
Var obj = {name: 'FNC ', age: 18, FNC: function() {console.log(this.name)}} // object.fnc (Copy the code
Other methods
DefineProperty (Object, property, descriptor) // Add or change multiple Object attributes Object.defineProperties(Object, property, descriptor) Descriptors) / / access attributes Object. GetOwnPropertyDescriptor (Object, Return all property) / / to array attribute Object. GetOwnPropertyNames array (Object) / / to return all the attribute of an enumerable Object. The keys (Object) / / access the prototype Object.getprototypeof (Object) // Prevent adding properties object.preventExtensions (Object) // If properties can be added to the Object, Returns true Object.isextensible (Object) // Prevents changing Object properties (not values) Object.seal(Object) // If the Object is sealed, Return true object.issealed (Object) // prevent any changes to the object.freeze (Object) // return true object.isfrozen (Object) if the Object isFrozenCopy the code

Attribute metadata

Enumerable: True // The property enumerable: true // the property can be configured againCopy the code

Object accessor

Getters and setters allow you to define object accessors (properties to be evaluated).

Getter (get keyword)
Var obj = {name: 'console.log ', age: 18} var obj = {name:' console.log ', age: 18} var obj = {name: 'console.log ', age: 18Copy the code
Setters (set keywords)
Var obj = {name: 'console.log ', age: 18, sex: ''} var obj = {name:' console.log ', age: 18, sex: ''} 18, sex: "female "}Copy the code
Get and set in the function
Var obj = {name: 'zhang3 ', age: 18, sex: ", get getName() {console.log(' its name is: '+ this.name) return' its name is: ' + this.name }, Set setSex(sex) {this.sex = sex console.log(this.sex)}} obj.setsex = 20 // 20 obj.getName(Copy the code
defineProperty

The object.defineProperty () method directly defines a new property on an Object, or modifies an existing property of an Object, and returns the Object. Three parameters: the object for which the property is to be defined, the name of the property to be defined or modified, and the content of the property to be defined or modified.

DefineProperty (obj, 'setSex', {set: function(val) { this.sex = val } }) Object.defineProperty(obj, 'getName', { get: Function () {console.log('name: '+ this.name)}}) obj.setSex =' male 'console.log(obj) // {name: "", sex: "Male "} obj.getName // name: zhang SANCopy the code
getOwnPropertyDescriptor

Object. GetOwnPropertyDescriptor () method returns the specified Object on a has its own corresponding attribute descriptor. Two parameters: the object to be found and the property of the object to be found. The return value:

  • Value: Indicates the value of the attribute
  • Writable: True if and only if the value of a property can be changed
  • Get: Gets the accessor function (getter) for this property
  • Set: Gets the setter function for this property
  • Enumerable: True if and only if the specified object’s property description can be changed or the property can be deleted.
  • Different: Indicates a different object if and only if the attributes of the specified object can be enumeratedtrue.
Var obj = {name: 'zhang' sex: 'male'} var desc = Object. GetOwnPropertyDescriptor (obj, 'name') console. The log (desc) / / {value: "Cx ", writable: true, Enumerable: true, different: true} console.log(descs. value) // CX works without any additional informationCopy the code

Object constructor

One way to create an “object type” is to use the object constructor function. Calling the constructor function with the new keyword creates an object of the same type.

function Person (name, age, sex) { this.name = name this.age = age this.sex = sex } var p1 = new Person('A', 15, 'female') to the console. The log (p1) / / Person {name: "A", the age: 15, sex: "female"}Copy the code
This points to the

The value of this, when used by the object, is the object itself. In the constructor, this has no value. He’s a replacement for the new guy. When a new object is created, it points to the new object.

function Person (name, age, Sex) {this.name = name this.age = age this.sex = sex console.log(this) this.getName = function() {console.log('name: '+ this.name)}} var p1 = new Person('A', 15,' female ') console.log(p1) // Person {name: "A", age: 15, sex: SetSex = function(sex) {this.sex = sex console.log(this.sex)} p1.setsex (' male ') p1.getName()Copy the code