Native JavaScript basic knowledge points (2) Review and review

Source: Internet
Author: User

Common objects

The reference object was mentioned in the last lesson, and a general talk was made. Here are 3 more of them to speak a little more detailed.

Object

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

Creating objects

There are two common ways to create an object, either by the object literal or by the {} new operator. As follows:

var obj = {};varnewObject();

The value of a key-value pair for the object content can be of various types of data, such as:

var obj = {    ‘string value‘,    123,    key3: {},    key4: [],    function () {}};
Get and set property values

Getting a property of an object can be implemented by an . operator or an [] operator, such as for the above object:

console// "string value"console.log(obj[‘key2‘// 123

The difference between the two approaches is that the latter will index the value of its internal expression, which will directly index the following content:

var‘key1‘,    ‘key‘;console// undefinedconsole// "string value"console// undefinedconsole‘1‘// "string value"

For setting property values, you can assign an attribute directly to a property, such as the following:

‘test‘;obj[‘key7‘‘test2‘;var‘key8‘‘test3‘;
Array

An array, similar to an object's structure for storing data, but whose contents are accessed sequentially through subscripts, rather than unordered key-value pairs.

Create an array

There are two common ways to create an array, by means of an array literal [] or an new operator. As follows:

var arr = [];varnewArray();

The data within an array can be of various types, such as:

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

Like an object's index, the array [] gets and sets the contents through the operator, but the index is replaced with subscript. The index of the array is incremented from the 0 beginning, such as for the above array:

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

Setting content is a similar way of assigning a value to an array of content. However, when the current label is larger than the array length, the length of the array is automatically extended to the size that can be accommodated. Such as:

arr[0‘test‘;console// 5arr[9‘test2‘;console// 10
Function

A function is a container for encapsulating content.

Create a function

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

function f() {    // code}varfunction () {    // code}

The function ultimately depends on the code, such as the function of the following to output a line of string:

function fun() {    console.log(‘hello world‘);}
Calling functions

函数名()call a function in the same way. As the above function:

// "hello world"
return value of the function

The return value is a result value that is obtained when the function is called. returnyou can set the return value of a function by keyword. The default return value is undefined .

console// undefinedfunction fun2() {    return123;}console// 123
Parameters

Parameters are the arguments that are externally passed to functions that can be used. When declaring a function, you can add parameters in parentheses. Such as:

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

Inside the function there is an automatically created array called arguments , which contains all the arguments passed in when the function is called, such as:

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

This array is useful in situations where some incoming parameters are indeterminate.

Little Practice
    1. Creates a function add that automatically adds and returns the incoming digits.
    2. Creates a function concat that links the two strings passed in.
Process Control

The process control in programming language is a very important thing, including judgment, circulation and so on. In particular, when we design the program, a clear flowchart can make subsequent development a lot 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}*/var100;if90) {  console.log(‘优秀‘elseif60) {  console.log(‘良好‘else {  console.log(‘不及格‘);}
Switch

The expression is judged and branched. It is easier to write than multiple parallel if :

/*switch (Expression) {    case Condition or value:        do some stuff;        break;    default:        do some default stuff}*/switch (true) {    case90:        console.log(‘优‘);        break;    case70:        console.log(‘良‘);        break;    default:        console.log(‘差‘);}
For

Use conditional judgment to control the loop to handle certain things.

/*for (A;B;C) {    do some stuff}其中 A 为初始化的内容,B 为循环的判断条件,C 为每次循环执行后会执行的代码*/for (var010; i++) {    console.log(i);}
While

Use conditional judgment to control the loop to handle certain things.

/*while (Condition)    do some stuff}Condition 即为循环的判断条件*/var0;while10) {    console.log(i);    i++;}
Do While

Use conditional judgment to control the loop to handle certain things. whilethe difference is that the code is executed first and do then the condition is judged. That is, the content that is executed at least once do .

/*do {    do some stuff} while (Condition);Condition 即为循环的判断条件*/var0;do {    console.log(i);    while10);
For in

Used to traverse all indexes in an object. Such as:

var obj = {    ‘value1‘};for (varin obj) {    console.log(‘Key=‘‘ Value=‘ + obj[item]);}
Break

Used to jump out of the nearest struct. Like end loop, jump out switch :

for (var010; i++) {    if5) {        break;    }    console.log(i);}
Continue

Used to end this cycle and go directly to the next loop, such as:

for (var010; i++) {    if5) {        continue;    }    console.log(i);}

Native JavaScript basic knowledge points (2) Review and review

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.