1.1 Overview of objects

An object is an unordered collection of data defined by curly braces, consisting of key-value pairs separated by colons. If an object contains multiple key-value pairs, each key-value pair is separated by commas (,). The last key-value pair does not end with a comma. Use a semicolon at the end of the brace to indicate the end of the object definition.

Objects are the core concept of JavaScript and the most important data type. All data in JavaScript can be treated as objects. In addition, JavaScript allows you to customize objects.

var obj = { key : value }; The above code defines an object that is assigned to the variable obj. A key B value C key D value

1.2 kin name

Key names are also called properties, and all properties of an object are strings, so they can be quoted or not. However, if the attribute does not meet the criteria for an identifier (for example, if the first character is a number, or if it contains a space or operator), it must be quoted.

var obj = {
    '1p': "Hello World",
    'h w': "Hello World",
    'p+q': "Hello World"
};
Copy the code

The three attributes of the object above do not qualify as identifying names, so they must be quoted. Check if the object contains an attribute ‘key’ in obj 1.2 Operation 1 for object properties. Check the key name, not the key value. Return true if contained, false otherwise.

Var girlFriend = {girlName:" Dilirba ", age:18}; console.log("age" in girlFriend); //true console.log("height" in girlFriend); //falseCopy the code

1.3 Object Reference

If different variable names refer to the same object, they are called references to that object.

That is, these variables all point to the same memory address, and changing the value pointed to by one variable affects the value pointed to by all the other variables.

var obj = { fire : 'burn' }; var people1_fire = obj; var people2_fire = obj; Obj. Fire = 'out '; console.log(people1_fire.fire); / / put out the console. The log (people2_fire. The fire); / / outCopy the code

2.1 Overview of functions

Official: A reusable block of code that a function is event-driven or executed when it is called.

Individuals: A function is a set of closed blocks of code that can be called repeatedly and has some specific functionIn the picture above [baozi making machine] can be regarded as a function.

Have certain specific function: can wrap bun

Capable of being called repeatedly: The machine can be used more than once

Closed: The machine on the outside can’t see what’s inside

We don’t care what’s inside the function, we just care what the function can do. (Use the machine to get the bun)

Function meaning: When a large number of programs are the same, it can be encapsulated as a function, so that many statements can be executed only once.

2.2 Declaration of functions

Function Function name (parameter 1, parameter 2...) {function body}Copy the code

Define a function. Use the keyword function to define a function. The statement that represents the definition in here does something. Function is followed by a space followed by the function name. The function name is also a keyword, and the naming convention is the same as variable naming. The name is followed by a pair of parentheses to place the parameters, which we’ll cover in a moment. And then the curly braces, and inside the curly braces are the statements of the function.

If the function is not called, then the statement inside will never be executed, not to call is useless writing.

function hello(){
      console.log("hello");
}
Copy the code

2.3 Function expression method

Var name = function(parameter 1, parameter 2…) {function body}; This way of declaring a function does not have a function name, but uses variables to refer to the function. The function is called by accessing the variable name.

var hello = function(){
    console.log("hello");
};
Copy the code

In contrast to the variety of function declarations, the way functions are called is much simpler. Normally, you can call a function by writing its name and parameters as long as the function has been declared.

The function name ();

Once a function is called, the statements inside the function are executed. You can feel that a function is a collection of statements, making statements into a legion, fighting collectively. All or nothing, all or nothing. Only when called.

   function fun1(){
       console.log(10);
    }
Copy the code

function fun1(){ console.log(20); } fun1(); It is obvious from the call that the result of repeating a function of the same name many times is that the function declared later overwrites the function declared earlier.

It is also permissible for javascript to write function calls before function declarations, because javascript has an implicit function promotion.

Before execution, there will be a pre-parsing process, promoting all function declarations to the very beginning, and then executing the first line of statements.

So, it doesn’t matter where the function is defined, the program will always find it.

fun1();
function fun1(){
    console.log(10);
}
Copy the code

Function declarations are promoted, but function expressions are not

fun(); Var fun = function(){var fun = function(){alert(" I am a function, I execute! "); ); }Copy the code

2.4 Types of functions

2.4.1 From the perspective of definition

Built-in function: refers to the built-in functions of JS, such as alert, prompt, parseInt and other user-defined functions. It refers to the functions defined by the user according to the actual situation

There are no parameters or parameters

2.4.3 In terms of return value

No return value function, return value function

2.4.4 other

Callback function, anonymous function, self-executing function

2.5 Parameters of a function

All the statements that are defined inside the function are the same, but we can actually make them different by using this thing called parameters.

When we define a function, the internal statement may have some variables that are pending. These variables are required to be listed in parentheses:

Function fun(a){console.log(" I "+ a +" I love you "); }Copy the code

When the function is called, the actual value of the variable is written in parentheses, so that the value is passed to a as the function is called:

fun(88);
Copy the code

A variable that is declared by var in a function is called a local variable and only defined in a function.

● Global variables defined in the global scope, not written in any function, are called global variables.

2.63 If you do not write var, it automatically becomes a global variable

function fn(){ a = 1; } fn();} fn();} fn(); console.log(a);Copy the code

If an identifier is encountered, it has never been var, and is assigned:

absdf = 123; This will automatically define the var ABSDF globally for you;

Tell us the truth, variables should be honest var.

2.6.4 Function parameters are defined as local variables of the function by default

function fn(a,b,c,d){

}
Copy the code

A,b,c, and d are local variables inside fn, which are undefined outside fn.

function fun(a){
    console.log(a);
}
fun(10);
console.log(a)
Copy the code