Object
A JavaScript object is an unordered collection data type that consists of several key-value pairs.
JavaScript objects are used to describe an object in the real world. For example, in order to describe "Xiao Ming" The naughty child, we can use a number of key-value pairs to describe him:
var xiaoming = { ' xiaoming ', 1990, ' Middle School ', 1.70, n/ A,null};
JavaScript uses a {...} representation of an object, and a key-value pair is xxx: xxx declared, separated by a form , . Note that the last key value pair does not need to be added at the end , , if added, some browsers (such as the lower version of IE) will error.
The above object declares a name property, the value is ‘小明‘ , the birth property, the value is 1990 , and some other properties. Finally, after assigning this object to a variable xiaoming , you can xiaoming get the properties of Xiaoming by using the variable:
// ' Xiao Ming ' // 1990
The Access property is . done through the operator, but this requires that the property name must be a valid variable name. If the property name contains special characters, it must be ‘‘ enclosed :
var xiaohong = { ' Little Red ', ' middle-school ': ' Middle School '};
xiaohongProperty name middle-school is not a valid variable, it needs to be ‘‘ enclosed. Accessing this property also fails to use the . operator and must be accessed by [‘xxx‘] :
// ' The Middle School ' // ' Little Red ' // ' Little Red '
can also be used xiaohong[‘name‘] to access xiaohong the name properties, but xiaohong.name the wording is more concise.
When we write JavaScript code, the property name tries to use the standard variable name as much as possible, so that a property can be accessed directly through object.prop the form.
Virtually all the properties of a JavaScript object are strings, but the value of the property can be any data type.
What does it return if a property that does not exist is accessed? JavaScript specifies that access to non-existent properties is not an error, but rather a returnundefined
var xiaoming = { ' xiaoming '};
Xiaoming.age; Undefined
Because JavaScript objects are dynamic types, you are free to add or remove attributes to an object:
varXiaoming ={name:' Xiao Ming '};xiaoming.age; //undefinedXiaoming.age = 18;//Add an Age propertyXiaoming.age;// -DeleteXiaoming.age;//Delete Age PropertyXiaoming.age;//undefinedDeletexiaoming[' name '];//Remove the Name propertyXiaoming.name;//undefinedDeleteXiaoming.school;//Deleting a non-existent school property will not error
If we want to detect if we xiaoming have a property, we can use the in operator:
var xiaoming = { ' xiaoming ', 1990, ' Middle School ', 1.70, n/ A,null}; inch // true inch // false
Be careful, however, that if in a property is judged to exist, this property is not necessarily xiaoming the case, it may be xiaoming inherited:
inch // true
Because toString it is defined in an object object, and all objects end up pointing at the prototype chain object , xiaoming They also have toString properties.
to determine whether a property is xiaoming owned by itself, but not inherited, you can use the hasOwnProperty() method :
var xiaoming = { ' xiaoming '};xiaoming.hasownproperty (// true // false
JavaScript Basics (6) object