Beginner JavaScript (2)

Today continues with the notes from the previous issue

Wait, I
recommend two websites,
blogs,
forums, and forums. If you
are interested, you can come and have a look, but don't attack, spam servers can't stand it!

Back to the theme

1. JavaScript objects

In JavaScript, objects are very important. When you understand objects, you can understand JavaScript.
The object is also a variable, but the object can contain multiple values ​​(multiple variables).

var car = {
    
    type:"Fiat", model:500, color:"white"};

In the above example, 3 values ​​("Fiat", 500, "white") are assigned to the variable car.
In the above example, 3 variables (type, model, color) are assigned to the variable car.
Object methods
Object methods define a function and are stored as properties of the object.
Object methods are called by adding () (as a function). This instance accesses the fullName() method of the person object:

name = person.fullName();

2. JavaScript function A
function is a code block wrapped in curly braces, and the keyword function is used before:

function functionname()
{
    
        // 执行代码
}

When the function is called, the code inside the function will be executed.
The function can be called directly when an event occurs (such as when the user clicks a button), and it can be called from any location by JavaScript.
Parameter function
When you call a function, you can pass values ​​to it. These values ​​are called parameters.
These parameters can be used in functions. You can send as many parameters as you want, separated by commas (,)

function myFunction(var1,var2)
{
    
    
代码
}

The return value can be achieved by using the return statement. When using the return statement, the function will stop execution and return the specified value.

JavaScript local variables Variables
declared inside JavaScript functions (using var) are local variables, so they can only be accessed inside the function. (The scope of this variable is local). You can use local variables with the same name in different functions, because only the function that declares the variable can recognize the variable. As soon as the function finishes running, the local variables will be deleted.
JavaScript global variables The variables
declared outside the function are global variables, and all scripts and functions on the webpage can access it.
such as

var var1 = 1; // 不可配置全局属性
var2 = 2; // 没有使用 var 声明,可配置全局属性
console.log(this.var1); // 1
console.log(window.var1); // 1
delete var1; // false 无法删除
console.log(var1); //1
delete var2; 
console.log(delete var2); // true
console.log(var2); // 已经删除 报错变量未定义

JavaScript operators
Addition, subtraction, multiplication and division are the same as in other languages, here I won’t talk nonsense about
JavaScript conditional statements

  1. if statement-use this statement to execute the code only when the specified condition is true
  2. if...else statement-execute code when the condition is true, and execute other code when the condition is false
  3. if...else if...else statement-use this statement to select one of multiple code blocks to execute
  4. switch statement-use this statement to select one of multiple code blocks to execute

loop statement

  1. for-loop the code block a certain number of times
  2. for/in-loop through object properties
  3. while-loop the specified code when the specified condition is true
  4. do/while-also loop the specified code block when the specified condition is true

while loop
while loop will loop through the code block when the specified condition is true.

while (条件)
{
    
        
需要执行的代码
}

do/while loop The
do/while loop is a variant of the while loop. The loop will execute the code block once before checking whether the condition is true, and then if the condition is true, the loop will be repeated.

do
{
    
       
 需要执行的代码
 }while (条件);

This is the end of today’s notes, and the next issue will continue

Guess you like

Origin blog.csdn.net/weixin_46678271/article/details/107212046