Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the entry points in JavaScript?

2025-04-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail what are the entry-level knowledge points in JavaScript. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

What is JavaScript?

JavaScript is a scripting language that runs on the client side

Basic input and output statement function statement printout console.log () pop-up output box alert pop-up input box prompt file written to [xss_clean] (')

Little theory:

Console.log can output any type of data, alert can only output data of type String, and can only output the first data, if the alert output is an object, it will automatically call the toString () method.

Variable

Declare the variable var (name)

Some points for attention

Multiple variables must be separated by commas when declared at a time, and must be wrapped, and multiple declared variables written on the same line will be invalid.

The direct output of the uninitialized variable is undfined

Naming convention for variables: alphanumeric underscore dollar sign (does not start with a number)

In variable initialization, there is no difference between single quotes and double quotes

Data type

JavaScipt is a > dynamic / weakly typed language

Num numeric Boolean Boolean Str string Undefined unknown value Null null

The variable data type of js is determined only by the value to the right of the equal sign while the program is running, also known as the dynamic data type.

Common sentences:

IsNAN () / / determines whether the value is a non-numeric / / escape character:\ n newline\ t indent\ b space str.length / / get the string length

Small theory

Undefined is added to the number, and the result is NaN

Null+1 equals 1

The value taken by prompt is character type.

For the addition of prompt values, there are the following examples:

/ / demo onevar a, b; a = parseInt (prompt ('please enter the first value'); b = parseInt ('please enter the second value'); var c = a + b; alert (c); / / demo two var a = prompt ('please enter the first value'); var b = prompt ('please enter the second value') Var c = Number (a) + Number (b); alert (c)

String conversion (chrom color is black)

Variable .toString ()

String () cast

Implicit conversion: + splicing

String template splicing my age is ${age} (be careful not to drop the backquotes)

Digital conversion (chrom color is blue)

Convert Parselnt () to integer

Convert parseFloat () to floating point

Number () cast function

Implicit conversion:-* / arithmetic implicit conversion

Boolean conversion (chrom color is blue)

Null and negative will be converted to false: such as'', 0, NaN, null, undefined

The rest are all true.

The operator = = converts the numeric type by default, which converts the character type to numeric type = congruent, requiring both the numerical value and the data type to be the same.

Priority:

Parenthesis

Monocular (right combined with right to left)

Do the math * /%

Displacement

Relationship

Equal

Logic & ^ | & & | |

Assignment

Comma

Select statement

If has nothing to say.

Switch considerations:

The value judgment of case is congruent operation judgment.

Array

How to create an array

1. Using new to create arrays

Var arr = new Array () / Note that new A must capitalize var arr = new Array (2); / / indicates the data length 2var arr = new Array (2, 3); / / indicates that there are two elements in it are 2 and 3

two。 Create an array using literals

Var arr = []

3. Get the length of the array

Arr.length

You can also modify the array length by arr.length = (Number)

Note:

Redundant address / empty address / undefined array element defaults to undefined

In C language, for example, character arrays end with a\ 0 by default, and subscript crossing the boundary will lead to program errors. The advantage of JS is that the length of the array can not be defined, or the address space can be given in advance.

4. Implement array migration so that there is no need to define index / subscript variables

NewArry [new.Arry.length] = arr [iTunes +]; common built-in objects of the array

1. Judgment array

/ / var arr = []; arr instanceof Array / / Instanceof operator determines whether it is an array Array.isArray (arr) / / isArray determines whether it is an array

two。 Add array elements

Arr.push () / / add one or more array elements to the end of the array arr.unshift () / / add one or more array elements to the front of the array

Practical: push can assign values to new empty arrays, and both push and unshift have return values, which is the length of the new array.

3. Delete array elements

Arr.pop () / delete the last element in the array arr.shift () / delete the first element in the array

There is a return value, which is the deleted element value

4. Flip / invert array

Arr.reverse ()

5. Array sorting

Arr.sort () / / Bubble sort a pair of digits

Theory: why bubble sorting of individual digits? because the sort comparison array converts the array to a string first, 77 will be earlier than 8, but if you specify the comparison function (compareFunction), you can arrange it as required.

Arr.sort solution

Arr.sort (function (a) b) {return a-b; / / return b-a

A-b is ascending sort, b-an is descending sort

6. Find array

Arr.indexOf ('word')

Returns the index number of the first element of the array that satisfies the condition from the search after going, or-1 if it cannot be found

Arr.lastIndexOf ('word')

Find the index number that returns the first element of the array that meets the condition from back to front, or-1 if it cannot be found.

7. Convert to string

Convert the arr.toString () / / array to a string arr.join () / / convert the array to a string, and a delimiter can be written in parentheses to indicate what symbol format is used to separate arr.join ('&')

Arguments pseudo arrays can only be used in functions

The data is also stored in the way of subscript, and the formal parameter can be received without defining the length.

Function

Declare the function:

Function function name () {}

Var variable name = Function () {} function expression (anonymous function)

Pre-parsing:

Is to bring all the var and function in JS to the front of the current scope

Variable pre-analysis

Advance all variable declarations (not to mention assignment operations)

Function pre-analysis

All function declarations are made in advance (no function calls)

→ should pay extra attention to the pre-parsing of function expressions and declaring functions.

Theory:

If the formal parameter is more than the actual parameter, press the front; if any argument has not been passed, the default is undefined

Return returns a value, but if return [] and multiple expressions are separated by commas, the return value will be the result of all operations for the entire []

!! If the function does not return a value, it returns undefined!

Variables assigned directly within a function belong to global variables

The formal parameter of a function is a local variable

Block-level scope: {} (es6 is added, other versions are specified in parentheses

Semantic variables can also be called outside)

Scope chain: when multiple functions are nested, a scope chain is formed, and the child function can access the block scope of the parent function.

Object

What is the object?

An object is an unordered collection of related properties and methods (strings, numeric values, arrays, functions)

In other languages, it is similar to structure > dictionary.

Create custom objects and object properties, object methods:

Literal quantity creation

Var obj = {name:' name', age: 999, / / * method * colon followed by * anonymous function * printf:function () {console.log ('hello world');}}

Memory method ↓:

Give object name, attribute colon and value + statement end comma directly

Method use colons

Value: object name. Property name or object name ['attribute name']

Call object method: object name. Method name ()

New object

Var obj = new Object (); obj.name = 'Leon'; obj.age = 999posiobj.printfdrug function () {console.log ('hello world');}

Memory method ↓:

Var object name = new Object (); without semicolon

Object name. Attribute = value + semicolon

Method use colons

Value: object name. Property name or object name ['attribute name']

Call object method: object name. Method name ()

3. Constructor function

Function Obj (Uname, Uage) {this.name = Uname;this.age = Uage;this.printf = function (infoms) {console.log (infoms);}}

Memory method ↓:

Similar to the function declaration, but the constructor function name must be capitalized, this. Attribute

The method uses the equal sign.

The constructor must have a variable value, and the method of the constructor must also have a variable value.

Call constructor var variable = new constructor name (passed value)

Var myobj = new Obj ('Leon', '999'); / / it can be understood to create a variable that specifies the constructor console.log (myobj.name); console.log (myobj [' age']); obj.printf ('hello world')

The execution process of new

The new constructor creates an empty object in memory

This will point to the object you just created.

Then execute the properties, methods, and return

New loop traversal

For (variable in object) {

}

The variable here is the property value for traversal.

Output of the value of the attribute: consolo.log (object [variable])

Programmers' variables in for in like to write Key or I

Some of the available built-in objects:

Math.PI / / returns the maximum value of pi Math.max () / / returns the maximum value (negative infinity if no value is returned) Math.abc () / / takes the absolute value Math.floor () / / rounds down Math.ceil () / / rounds up Math.round () / / rounds up Math.round () / / rounding it up.

Random number

Math.random ()

Returns a random floating-point number [0Power1) without parameters in parentheses

↓ extension: get a random function between the custom maximum and minimum values

Function getRandomInt (min, max) {min = Math.ceil (min); / / canonical value statements can save max = Math.floor (max); / / canonical value statements can save return = Math.floor (Math.random () * (max-min + 1) + min)

The new Date () call date object does not return the system time with the parameter

('2021-10-10-10-8-0-8-1-0-8-0-1-8-0-1-0-1-4') string output

(2021, 10, 10) the digital output month starts at 0 and ends at 11, so it has to be + 1 on the basis of the month.

The variable .getDay () gets the current week, and the value obtained on Sunday from 0 to Saturday 6 is a number.

The ↓ specification gets the current time demo

Var date1 = new Date;var year = date1.getFunllYear (); var month = date1.getMonth (); var dates = date1.getDate (); var day = date1.getDay (); var arr = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] Alert ('Today's date is:' + year + 'year' + (month+1) + 'month' + dates + 'day' + arr [day])

It is worth noting that the constructor Date must be called with a variable

Basic packing type:

Is to package simple data types into complex data types, so that basic data types have properties and methods. Generally speaking, the JS engine will automatically package simple data types into complex data types for us in browsers.

Var str = 'lion'; console.log (str.length); / / how JS wraps our data var temp = new String (' lion'); / / defines a temporary constructor containing the initialized string str = temp; / / assigns the constructor to the value type temp = null; / / and finally discards the temporary variable

Some available string built-in objects

Find string

Str.indexOf ('string to find', [starting position]); / / demo:var str = 'what my name is, it doesn't matter'; console.log (str.indexOf ('name', 3))

Find the specified character

Str.charAt (0); / / returns the character str.charCodeAt (0) of the specified subscript; / / returns the ASCII code of the specified subscript

Concatenate string

Str.concat ('word') / / stitching strings (covert stitching is most common in development)

Find a continuous character

Str.substr ('grab starting position', 'grab a few characters'); / / find a continuous character

Replacement character

Str.replace ('replaced character', 'replace with character') / / if there are duplicate characters in the string, only the first character will be replaced

Convert a string to an array

Split ('delimiter') / / converts a string to an array (the delimiter must be written in the converted string and consistent with the delimiter of split!)

Some parameters and types of-

In-depth theory:

A simple type is also called a basic type or a value type because the storage is the value itself.

The system automatically allocates and releases the value of the storage function, the value of the local variable, which is called the stack

A complex type is also called a reference type because it stores an address call that requires a special reference.

Now there is a hexadecimal address code in the stack, and then it points to the data (object instance) in the heap, which is called heap complex type parameter transfer. If it is not a value, the pointer, that is, the address.

Special: Null returns an empty object (because of a design flaw in the program)

You can define the variable as Null and give the value later.

This is the end of this article on "what are the entry points in JavaScript?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report