2016.9.1 JavaScript Introductory Five

Source: Internet
Author: User
Tags hasownproperty value store

1. Data type: object; it can also be thought of as a key/value store, like a dictionary. Can replace switch{case:case:} or if Else

For example:

Function Phoneticlookup (val) {
var result = "";

var lookup = {

Alpha: "Adams",

Bravo: "Boston",

Charlie: "Chicago",

Delta: "Denver",

Echo: "Easy",

Foxtrot: "Frank"

};
result = Lookup[val];


return result;
}

Phoneticlookup ("Charlie");

2. It is sometimes useful to check whether the properties of a given object exist or do not exist. We can use the object of the hasOwnProperty (propname) method to have the property name of a particular object;

For example:

var myObj = {
Gift: "Pony",
Pet: "Kitten",
Bed: "Sleigh"
};

function Checkobj (checkprop) {
if (Myobj.hasownproperty (Checkprop)) {
var result= "";
Result=myobj[checkprop];
return result;
}else{
Return "Not Found";
}
}

Checkobj ("house");

3. A magical way to store data is to use JSON:

This data structure can even be used as an element in an array, for example:

var mymusic = [
{
"Artist": "Billy Joel",
"title": "Piano Man",
"Release_year": 1973,
"Formats": [
"CS",
"8T",
"LP"],
"Gold": True
},//First array element
{
"Artist": "Billy Joel",
"title": "Piano Man",
"Release_year": 1973,
"Formats": [
"CS",
"8T",
"LP"],
"Gold": True
}//second element of the array
];

4. Provide two ways to access objects:

var mystorage = {
"Car": {
"Inside": {
"Glove box": "Maps",
"Passenger seat": "Crumbs"
},
"Outside": {
"Trunk": "Jack"
}
}
};

var gloveboxcontents =mystorage.car.inside["glove box"];

Ii:

var myplants = [
{
Type: "Flowers",
List: [
"Rose",
"Tulip",
"Dandelion"
]
},
{
Type: "Trees",
List: [
"Fir",
"Pine",
"Birch"
]
}
];

var secondtree =myplants[1].list[1];

6.parse is used to parse a JSON object from a string, such as:

7.stringify () is used to parse out a string from an object, such as:

8. In JavaScript, a common way to iterate through an array is to use a For loop:

9. The traversal of multidimensional arrays is often implemented in nested loops and is used in the same way as in JavaScript and overlays:

The 11.while loop is executed only when the condition is true, and once the condition is false, the execution is completed:

hasOwnProperty: is used to determine whether an object has a property or object that you give a name to. However, it is important to note that this method cannot check whether the object has this property in its prototype chain, and that the property must be a member of the object itself.

isprototypeof: is used to determine whether the object whose prototype chain is to be checked exists in the specified object instance, or True, otherwise returns false.

Example: An array name for an object [I]. Object properties, iterating through the objects in the array;

2016.9.1 JavaScript Introductory Five

Related Article

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.