Multiple definitions and calling methods of functions

1. How the function is defined

(1) Function keyword (named function)
function fn(){
    
     }
(2) Function expression (anonymous function)
let fun=function(){
    
    }
(3)new Function()
let fn=new Function('参数1','参数2','函数体');
fn();
  • The functions in Function must be in string format
  • The third method is inefficient and less used
  • All functions are instance objects of Function
  • Functions are also objects

2. How to call the function

(1) Ordinary function
function fn(){
    
    
		console.log(1);
		}
fn();
fn.call();
(2) Object method
let o={
    
    
	sayHi:function(){
    
    
			console.log(1);
			}
	}
o.sayHi();
(3) Constructor
function Star(){
    
     }
new Star();
(4) Binding event function
btn.onclick=function(){
    
    }
//触发
(5) Timer function
setInterval(function(){
    
    },1000)
//时间触发 一秒钟触发一次
(6) Execute the function immediately
(function(){
    
    
	console.log(1)})();
//自动调用

Guess you like

Origin blog.csdn.net/d1063270962/article/details/108664549