Basic native javascript knowledge point-review and review

Source: Internet
Author: User

Basic native javascript knowledge point-review and review
Common objects

I mentioned the reference object in the previous lesson and gave a general introduction. Here we will discuss three of them in a little more detail.

Object

Object in the form of a key-value pair, which is mainly used for storage and encapsulation.

Create object

There are two common ways to create an object:{}OrnewOperator. As follows:

var obj = {};var obj2 = new Object();

The key-value pairs of object content. The values can be various types of data, such:

var obj = {    key1: 'string value',    key2: 123,    key3: {},    key4: [],    key5: function () {}};
Get and set attribute values

You can use.Operator or[]Operator, for example, for the above object:

console.log(obj.key1); // "string value"console.log(obj['key2']); // 123

The difference between the two methods is that the latter uses the calculated value of its internal expression as the index, while the former directly uses the following content as the index:

var str = 'key1',    str2 = 'key';console.log(obj.str); // undefinedconsole.log(obj[str]); // "string value"console.log(obj[str2]); // undefinedconsole.log(obj[str2 + '1']); // "string value"

You can directly assign values to an attribute by setting the attribute value, for example:

obj.key6 = 'test';obj['key7'] = 'test2';var str3 = 'key8';obj[str3] = 'test3';
Array

An array is similar to a structure of an object that stores data. However, its content is accessed through subscript sequentially, rather than unordered key-value pairs.

Create an array

There are two common methods to create an array:[]OrnewOperator. As follows:

var arr = [];var arr2 = new Array();

The data in the array can be of various types, such:

var arr = [    'string',    123,    [],    {},    function () {}];
Get and set content

Similar to the object index[]Operator to obtain and set the content, but the index is replaced with a subscript. Array subscript from0Start to increase in sequence, for example, for the preceding array:

console.log(arr[0]); // "string"console.log(arr[1]); // 123console.log(arr[100]); // undefined

You can assign values to the array content in a similar way. However, when the subscript is greater than the array length, the length of the array is automatically extended to the size that can be accommodated. For example:

arr[0] = 'test';console.log(arr.length); // 5arr[9] = 'test2';console.log(arr.length); // 10
Function

A function is a container used to encapsulate content.

Create a function

There are two ways to create functions, named functions and anonymous functions:

function f() {    // code}var f2 = function () {    // code}

The final role of a function depends on the Code. For example, the function output a line of strings:

function fun() {    console.log('hello world');}
Call a function

PassFunction Name ()To call a function. The above functions:

fun(); // "hello world"
Function return value

The returned value is a result value obtained after the function is called. PassreturnKeyword can be used to set the return value of a function. The default return value isundefined.

console.log(fun()); // undefinedfunction fun2() {    return 123;}console.log(fun2()); // 123
Parameters

Parameters are some of the variables that can be used externally transmitted to the function. When declaring a function, you can add parameters to brackets. For example:

function sum(var1, var2) {    return var1 + var2;}console.log(sum(1, 2)); // 3

There is an automatically created array in the function calledargumentsIncluding all parameters passed in when calling a function, such:

function arg() {    console.log(arguments);}arg(1, 23, 'test'); // [1, 23, 'test']

This array is useful when the input parameters are unknown.

Create a function in the exercise. addIs used to automatically add and return incoming numbers. Create a function concatIs used to link two strings passed in. Process Control

In programming languages, process control is a very important thing, including judgment and loop. Especially when designing a program, a clear flowchart makes subsequent development much easier.

If

That is, to judge a condition, when it is true to do something, when it is false to do another thing:

/* If (Condition) {do some stuff} else {do some stuff} */var score = 100; if (score> 90) {console. log ('excellent ');} else if (score> 60) {console. log ('good');} else {console. log ('failed ');}
Switch

Judge an expression and process it in a branch. Compared with multiple parallelifEasier to write:

/* Switch (Expression) {case Condition or value: do some stuff; break; default: do some default stuff} */switch (true) {case score> = 90: console. log ('out'); break; case score> = 70: console. log ('loan'); break; default: console. log ('badge ');}
For

Use conditional judgment to control and process certain things cyclically.

/* For (A; B; C) {do some stuff} where A is the initial content and B is the judgment condition of the loop, C is the code that will be executed after each loop */for (var I = 0; I <10; I ++) {console. log (I );}
While

Use conditional judgment to control and process certain things cyclically.

/* While (Condition) do some stuff} Condition is the judgment Condition of the loop */var I = 0; while (I <10) {console. log (I); I ++ ;}
Do while

Use conditional judgment to control and process certain things cyclically. AndwhileThe difference is thatdoTo determine the conditions. At least oncedo.

/* Do {do some stuff} while (Condition); Condition is the judgment Condition of the loop */var I = 0; do {console. log (I); I ++;} while (I <10 );
For in

Used to traverse all indexes in an object. For example:

var obj = {    key1: 'value1'};for (var item in obj) {    console.log('Key=' + item + ' Value=' + obj[item]);}
Break

Used to jump out of the nearest struct. Such as end loop and jump outswitch:

for (var i = 0; i < 10; i++) {    if (i === 5) {        break;    }    console.log(i);}
Continue

Used to end this loop and directly enter the next loop, such:

for (var i = 0; i < 10; i++) {    if (i === 5) {        continue;    }    console.log(i);}

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.