JS basic finishing

Three ways to use JS

 

    1. Directly in the html tag, use the event attribute to call the js code

    <button onclick="alert('弹框')">弹框!</button>

  

    2. Use the script tag to insert js code anywhere on the page

    <script type="text/javascript">
    //js code
    </script>

  

  3. Introduce external js files

    <script src="new_file.js" type="text/javascript"></script>

  

    [Notes]
    ① js code can be used anywhere in the code, but the placement of the code will affect the order of js execution
    ② the script tag introduced into external js will no longer contain any js code

variables in js

  1. Variable declaration:

    var num=1;//Variables declared with var are local variables and are only valid in the current scope
    num="Penguin"//Variables declared without var are global variables by default, available in this js file
    var a= 1,b,c=3;//Use one line of code to declare multiple statements where b is undefined

  [Notes on variable declaration in js]
    ①There is only one variable keyword in js, and the type of variable depends on the assigned value
    If it is not assigned a value after the declaration, it is of undefined type

    ② the same variable in js can be modified in multiple assignments

    .
    Difference: The scope declared with var is a local variable

    ④ In js, a variable can be declared with var multiple times, and the declaration is equivalent to assignment.

    ⑤ js variable names are case-sensitive

  2. Data types in js:

 

    undefined: declared with var, but not assigned
    null: null reference
    Boolean: true or false
    number: numeric type, including integer and floating point
    string: string
    object: object

  3. Commonly used numerical functions

    ①isNaN: It is used to detect whether a variable is not a namber.
    When isNaN is detected, it will first call the number function to try to convert the variable to a numerical type. If the final result can be converted to a numerical value, it is not NaN

    . ②Namber function: the Various data types are converted to numeric type
    undefined and cannot be converted, return NaN
    null to 0
    Boolean true 1 false 0
    string If it is a pure numeric string, you can convert "123" --> 123
    If there are numbers and letters, it cannot be converted
    If it is empty Convert the string to 0

    ③parseInt(): Convert the string to a numeric type
    If the string is an empty string, it cannot be converted to NaN.
    If it is a pure numeric type, it can be converted, and the decimal point is directly rounded off "123.8" --> 123
    If the string If it contains non-numeric characters, convert the integer in front of the non-numeric characters to "1a1"-->1

    ④parefloat: The conversion mechanism is the same as that of pareint(
    )
    . >123

    ⑤typeof: Detect the data type of a variable
    String returns a string value returns a number true returns a Boolean undefined returns an undefined object/null returns an object function returns a function

Commonly used input and output statements in JS

 

    1. alert(): pop-up window output

    2. prompt: pop-up window input receives two parts of parameters: ① the input prompt content, ② the default content of the input box, both parts can be omitted.

    The input content is a string by default.

    3. document.write: print the content in the browser

    4. console.log: print the browser console

Function declaration and call in JS

 

    1. Format of function declaration     

   

 function function name(parameter 1, parameter 2) {
    // function body code
     return return value;
     }

     Function call:
     ①Direct call, function name (multiple parameters);
     ②Call through event,

<button onclick="func(prompt(),'444'))">Click you</button>

     2. Notes on function declaration and invocation: 

    ① Whether there is a return value in the function only depends on whether there is a return in the function. There is no need to declare that
     there is no return value, and it is received as undefined
     . ②In js, the formal parameter list and the actual parameter list of the function do not have any associated
     function parameters. The actual number of parameters depends on the actual parameter list
     . 3. In js, the function is the only scope of the variable:  ( Variables declared elsewhere are global variables)
     The formal parameters of the function are local variables belonging to the function

     4. There is no sequence between the declaration of the function and the calling statement. You can call it first and then declare it

     [Code execution order]
     js code execution is divided into two Phase, check compilation phase, code execution phase
     Check compilation phase: check syntax errors, declaration of variables, declaration of functions,
     code execution phase, assignment of variables, call execution of functions
    

    func();
   var func = function () {
    alert(99);
   }


    Execute var func first
    and then execute func();
    func=function(){
      alert(99);
     }
    so the function call should be placed after the declaration
    function func(){
      alert(1);
    }
      func();

   

Declaration and use of anonymous functions

 

     1. An anonymous function expression:
     

var func = function () {}

   
     2. Assign an anonymous function directly to an event
 

window.onload=function(){}//Document ready function to ensure that the code in the function is executed after the html is loaded
window.onload=function(){
  document.getElementById("div").onclick=function(){}
 }

 

     3. Self-executing function
     ①!function(){}(): Use at the beginning! Represents a self-executing statement 
     ② (function(){}()): Wrap anonymous function declarations and calls with
     () ③(function(){})(): Wrap anonymous function declarations with ()

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325164360&siteId=291194637