The difference between call, bind, apply

Features

bind\apply\call will change the point of this when the function is called

the difference

  • Execution method:
    apply\call will execute the function immediately, bind is the function that returns the binding this point,
  • Parameter
    bind\apply\call The first parameter is used for this to point to the changed object.
    Apply and the remaining parameters use array wrap parameters. The
    first parameter of bind and call is used for this to point to the changed object, and the remaining parameters are passed directly

Sample code

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>bind,call,apply的区别</title>
</head>
<body>
	<script>
		let obj = {
     
     
			name:"obj中的name",
			print(a,b,c){
     
     
				console.log(this.name,a,b,c)
			}
		}
		window.name = "window中的age";

		obj.print.bind(this,'1','2','3')()
		obj.print.apply(this,[1,2,3])
		obj.print.call(this,'1','2','3')
	</script>
</body>
</html>

Print result

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_35958891/article/details/107950397