JavaScript study notes seven-functions

One, function function

  1. A function is also an object, some functions (code) can be encapsulated in the function, and these functions (code) can be executed when needed.
  2. Some codes can be saved in the function to be called when needed.

Second, use the constructor to create a function object

We rarely use constructors to create function objects in actual development

var fun=new Function();
//构造函数来创建函数对象
  1. When using typeof to check a function object, function is returned.
  2. The code to be encapsulated can be passed to the constructor in the form of a string.
var fun=new Function("console.log('Hello 这是我的第一个函数');");
  1. The code encapsulated in the function will not be executed immediately

  2. The code in the function will be executed when the function is called

  3. Call function syntax: function object ()

  4. When the function is called, the code encapsulated in the function will be executed in order

var fun=new Function("console.log('Hello 这是我的第一个函数');");
fun();//调用函数
结果:Hello 这是我的第一个函数

Three, use function declarations to create functions

语法:
    function 函数名([形参1,形参2...形参n]){
    
    
             语句...(需要封装的代码)
}
语法:
    function fun2(){
    
    
        console.log('Hello 这是我的第二个函数');
}
//调用fun2
fun2();

Four, use function expressions to create a function

var 函数名= function([形参1,形参2...形参n]){
    
    
             语句...(需要封装的代码);
}
function(){
    
    
  console.log("我是一个匿名函数中封装的代码");
}

First create an anonymous function, then define a variable fun3, and then assign the anonymous function object to the variable.

//定义变量fun3保存函数对象
var fun3=function(){
    
    
  console.log("我是一个匿名函数中封装的代码");
}
fun3();

Five, the parameters of the function

  1. One or more formal parameters can be specified in the () of the function. Multiple formal parameters are separated by commas. Declaring a formal parameter is equivalent to declaring the corresponding variable inside the function, but not assigning a value.
    function sum(a,b){
    
    
        console.log(a+b);
}
  1. When calling a function, you can specify actual parameters in (). The actual parameter will be assigned to the corresponding formal parameter.
sum(1,2);//调用函数sum,指定实参
  1. When calling a function, the parser will not check the type of the actual parameter, so be careful, whether it is possible to receive an illegal parameter, you can check the parameter type.
  2. When calling a function, the parser will not check the number of actual parameters, and redundant actual parameters will not be assigned.
  3. If the number of actual parameters is less than the number of formal parameters, the new parameter without corresponding actual parameters will be undefined.
  4. The actual parameters of the function can be any data type.

Six, the return value of the function return

  1. You can use return to set the return value of the function
    2. Syntax: return value (variable);
  2. The value after return will be returned as the execution result of the function, and a variable can be defined to receive this result.
  3. In the function, the statement after return will not be executed
function sum(a,b,c){
    
    
				var d=a+b+c;
				return d;不写
			}
			//调用函数
			//变量result的值就是函数的执行结果
			//函数返回什么result的值就是什么
			var result=sum(1,2,3);
			console.log(result);
  1. If the return statement is not followed by any value, it is equivalent to returning an undefined. Not writing is also undefined.
    5. Any type of value can be returned after return. Can also be an object
function people(){
    
    
				  return {
    
    name:"lili"}; 	 
		  }
		 var a= people();//函数people作为实参传值
		 console.log(a);

Can also be a function

Seven, the actual parameter can be any value

  1. The actual parameter can be any data type or an object. When we have too many parameters, we can encapsulate the parameters into an object, and then pass the actual parameters through the object
 function people(o){
    
    //o是形参,obj是一个实参对象,obj将赋值给o
	console.log("我叫"+o.name+",我今年"+o.age+"岁了,我是"+o.gender+"生");
	 }
		  var obj = {
    
    
			  name:"lili",
			  age:22,
			  gender:"女"
		  };
		  people(obj);//将一个对象obj作为实参传入
  1. The actual parameter can be a bunch of objects or a function
  
function people(o){
    
    //o是形参,obj是一个实参对象,obj将赋值给o
			  console.log("我叫"+o.name+",我今年"+o.age+"岁了,我是"+o.gender+"生");
		  }
		  var obj = {
    
    
			  name:"lili",
			  age:22,
			  gender:"女"
		  }; 
		   people(obj);//将一个对象obj作为实参传入
 function fun(result){
    
    
			  //console.log("这是一个函数作为实参的例子:"+result);
			 result(obj);
		
		  }
		  fun(people);//函数people作为实参传值
  1. You can pass an anonymous function as an argument to another function.
function test(){
    
    }
test()是调用函数对象
fun(test(10))相当于fun这个函数对象调用了test函数以10为形参的返回结果
test是函数对象,相当于与直接使用这个函数对象
 function fun1(){
    
    
				  return {
    
    name:"lili"}; 	 
		  }
		  function fun2(a){
    
    
			  console.log( a.name); //lili
			  
		  }
		  fun2(fun1());//调用fun1函数对象的返回值

Eight, break and continue and return

  1. Break can exit the current loop, and none after the current loop times will be executed.
  2. continue is equivalent to skip the current loop, and continue to execute after skipping the current loop.
  3. return can end the entire function

Nine, execute the function immediately

function(){
    
    
  console.log("我是一个匿名函数中封装的代码");
}
//给匿名函数加一个括号表示一个整体
//在这个函数对象后面再加一个括号,表示调用这个函数对象,这是一个立即执行函数。
(function(){
    
    
  console.log("我是一个匿名函数中封装的代码");
})();

Immediate execution function: the function is defined and called immediately. This kind of function is called the immediate execution function, and the immediate execution function is usually executed only once.

Ten, the constructor class

  1. Create a constructor, specifically used to create a Person object, for all the different types in the object.
  2. The constructor is an ordinary function, and the creation method is no different from the ordinary one. The difference is that the first letter of the constructor is customarily capitalized.
  3. Ordinary function is called directly, and the constructor needs to use the new keyword to call
function Person(){
    
    	
			}
			//new构造一个新对象
			var per= new Person();
			console.log(per);//{}
  1. The execution flow of the constructor:
    (1) Create a new object immediately
    (2) Set the newly created object as this in the function, and use this to refer to the newly created object in the constructor. Per holds the address of the newly created object, and this points to the newly created object, which is the per object.
    (3) Execute the code in the function line by line
    (4) Return the object of the new key as the return value `
function Person(){
    
    	
     this.name="孙悟空";
     this.age=18;
     this.sayName=function(){
    
    
       console.log(this.name);
};
			}
			//new构造一个新对象
			var per= new Person();
			console.log(per);

Pass value:

function Person(name,age){
    
    	
     this.name=name;
     this.age=age;
     this.sayName=function(){
    
    
       console.log(this.name);
};
			}
			//new构造一个新对象
	var per= new Person("孙悟空",18);
	var per2= new Person("猪八戒",28);
			console.log(per);
  1. Objects created with the same constructor are called first-class objects. For example, per1 and per2 are objects of the same type, all of which belong to the person class. A constructor is also called a class.
  2. We will use a constructor to create an object called an instance of this class. For example, per is an instance of the Person class.
  3. Use instanceof to check whether an object is an instance. Syntax: if the object instanceof constructor returns true, not false
console.log(per instanceof Person);
  1. All objects are descendants of Object, so any object and Object will return true when doing instanceof check.

  2. Create a Person constructor. In the Person constructor, a sayName method is added for each object.

  3. At present, our method is created inside the constructor, that is, a new sayName method is created every time the constructor is executed, and the sayName of all instances is unique.

  4. This leads to the creation of a new method when the constructor is executed once, and how many new methods are created as many times as it is executed. These methods are all the same, which is completely unnecessary.

  5. Define the sayName method in the global scope

function Person(name,age){
    
    	
     this.name=name;
     this.age=age;
     this.sayName=fun;
			}
//将sayName方法放在全局作用域中定义
function fun(){
    
    
       console.log(this.name);
};
			//new构造一个新对象
	var per= new Person("孙悟空",18);
	var per2= new Person("猪八戒",28);
			console.log(per);
  1. But defining the function in the global scope pollutes the namespace of the global scope, and it is not safe to define it in the global scope.
  2. Take the study notes 9-prototype
	function Person(name,age){
    
    	
						     this.name=name;
						     this.age=age;
						   
									}

									//new构造一个新对象
							var per= new Person("孙悟空",18);
							var per2= new Person("猪八戒",28);
							Person.prototype.sayName=function(){
    
    
                                    console.log(this.name);
									}
							console.log(per.sayName());

Guess you like

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