JavaScript study notes eight-methods

1. Method

  1. Functions can also be used as properties of an object. If a function is stored as a property of an object, then we call this function a method of this object, and calling a function means calling a method of this object.
var obj=new Object();
		obj.name="lili";
		obj.age=22;
		obj.sayName=function(){
    
    
		   console.log(obj.name);
		}
	 obj.sayName();//lili调用了obj对象的sayName方法

Second, the attributes in the enumerated object

Enumeration: When you don't know what methods are in an object, you need to use enumeration objects to enumerate them.

Use the for...in statement.
Syntax: for (var variable in object) {}

for(var n in obj){
    
    
  console.log("hello");//obj里有几个属性,循环体就会执行几次
}

For...in statement, there are several attributes in the object, and the loop body will be executed several times.

Each time it is executed, the name of the property in the object is assigned to the variable

for(var n in obj){
    
    
  console.log(n);//输出obj对象中的所有属性名称
}
//属性名循环赋值给n

Reference study notes six variable names

for(var n in obj){
    
    
  console.log(obj[n]);//输出obj对象中的所有属性值,
}
//属性值循环赋值给n,因为n是变量,要用[]

Three, scope

Scope refers to the scope of a variable.
There are two types of scope in JS

(1) Global scope

  1. The JS code written directly in the Script tag is in the global scope
  2. Created when the global scope page is opened, and destroyed when the page is closed
  3. There is a global object window in the global scope, which represents a browser window. It is used directly by the browser and we can use it directly.
  4. The created objects will be saved as properties of the window object.
  5. The created function will be saved as a method of the window object.
  6. Variables in the global scope are global variables and can be accessed in any part of the page.

(2) The declaration of variables in advance

  1. Variables declared with the var keyword will be declared before all the code is executed, but will not be assigned, just the variable is declared first.
  2. If the var keyword is not used when declaring a variable, the variable will not be declared in advance, and the default is the window. variable. An error will be reported when outputting variables.

(3) The declaration of the function in advance

  1. The function function (){} created using the function declaration form will be created before all the code is executed, so we can call this function before the function declaration.
  2. The function created with the function expression var variable=function(){} will not be declared in advance, so it cannot be called before declaration.

(5) Local scope (function scope)

  1. The function scope is created when the function is called. After the function is executed, the function scope is destroyed.
  2. Every time a function is called, a new function scope is created, and they are independent of each other.
  3. In the function scope, you can access variables in the global scope. But it is the object in the global scope that cannot be accessed in the function scope.
  4. When a variable is manipulated in the scope of a function, it will first look for it in its own scope, if there is one, use it directly, if it does not look for the upper scope, until it finds the global scope, if the global scope is still not found, then Will report an error, referenceError.
  5. If you want to use global variables in a function, you can use windows. variables.

(6) Function scope declaration in advance

  1. In the function scope, there is also the feature of advance declaration. Variables declared with the var keyword will be declared before all the code in the function is executed.
  2. The function declaration will also be executed before all the code is executed

In the function, variables that are not declared with var will become global variables.
Defining formal parameters in a function is equivalent to declaring variables in the scope of the function.

Four, this

  1. The parser passes an implicit parameter into the function every time it calls the function, and this parameter is this.
  2. This points to an object, which we call the context object of function execution. According to the way the function is called, this points to different objects.
  3. When called as a function, this is always window.
  4. When called in the form of a method, this is the object that called the method
  5. When called in the form of a constructor, this is the newly created object.
  6. When using call and apply, this is the specified object.
  7. In the event, this represents the element that receives the event, whoever triggers the event, this is who

Guess you like

Origin blog.csdn.net/weixin_45636381/article/details/112850758