JavaScript Basics (iii) object condition judgment

Source: Internet
Author: User
Tags access properties hasownproperty

A JavaScript object is an unordered collection data type that consists of a number of key pairs.
var Guagua = {
Name: ' Melon ',
birth:1988,
School: ' The Middle school ',
height:1.70,
WEIGHT:65,
Score:null
};

JavaScript with a {...} Represents an object in which key-value pairs are declared in xxx:xxx form, separated by. 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 make an error.
The above object declares a name attribute, the value is ' xiaoming ', the birth property, the value is 1988, and some other properties. Finally, after assigning this object to the variable Guagua,
You can use the variable Guagua to get the attributes of Xiaoming:
Guagua.name; ' Melon '
Guagua.birth; 1988

Access properties are 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 in ":
var Tutu = {
Name: ' Tutu ',
' Middle-school ': ' The Middle school '
};

The property name of Tutu Middle-school is not a valid variable, it needs to be enclosed. Access to this property is also not possible with the. operator, which must be accessed using [' xxx ']:
tutu[' Middle-school ']; ' The Middle School '
tutu[' name ']; ' Tutu '
Tutu.name; ' Tutu '

Note: All properties of a JavaScript object are actually 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 attributes does not error, but rather returns undefined:
var Tutu = {
Name: ' Tutu '
};
Tutu.age; Undefined

Because JavaScript objects are dynamic types, you are free to add or remove attributes to an object:
var Tutu = {
Name: ' Tutu '
};
Tutu.age; Undefind
Tutu.age = 18; Added an Age property.
Tutu.age; 18
Delete tutu.age//Remove Age Property
Tutu.age; Undefinde
delete.tutu[' name ']//Delete Name property
Tutu.name; undefinded
Delete Tutu.school; Deleting a school attribute that does not exist at all does not have an error.

If we want to insist that tutu have a property, you can use the in operator.
var Tutu = {
Name: ' Tutu ',
birth:1988,
School: ' The Middle school ',
height:1.70,
WEIGHT:65,
Score:null
};
' Name ' in Xiaoming; True
' Grade ' in xiaoming; False

Note: But be careful, if in determines that an attribute exists, this property is not necessarily xiaoming, it may be inherited by Xiaoming:
' ToString ' in tutu; True
Because ToString is defined in object objects, and all objects end up pointing to object on the prototype chain, Tutu also has the ToString property.
To determine whether a property is owned by Tutu itself, but not inherited, you can use the hasOwnProperty () method:
var Tutu = {
Name: ' Tutu '
};
Tutu.hasownproperty (' name '); True
Tutu.hasownproperty (' toString '); False

Condition Judgment,
JavaScript uses if () {...} Else{...} To make conditional judgments.
var age = 20;
if (age >= 18) {//If the age >=18 is true, the IF statement is executed.
Alert (' adult ');

}else{//Otherwise execute ELSE statement block
Alert (' Teenager ')
}

Where else statements are optional and if the statement contains only one statement, you can omit the {}
var age = 20;
if (age >=18)
Alert (' adult ');
Else
Alert (' teenager ');

Multi-line condition judgment, if you want to judge the condition more carefully, you can use multiple if...else ... Combination of:
var age = 3;
if (age >=18) {
Alert (' adult ')
} else if (age >= 6) {
Alert (' teenager ');
}else{
Alert (' Kid ')
}

Above multiple If...else ... The combination is actually equivalent to two layers of if...else ... :
var age = 3;
if (age >=18) {
Alert (' adult ');
} else{
if (age >=6) {
Alert (' teenager ');
}else{
Alert (' Kid ')
}
}

Note: If...else ... The execution characteristic of the statement is two select one, in multiple if...else ... Statement, if a condition is true, subsequent judgments are no longer continued.
JavaScript treats null, undefined, 0, Nan, and empty string ' ' as false, and all other values are treated as true.

JavaScript Basics (iii) object condition judgment

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.