<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=<device-width>, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function demo(){
console.log(this);
}
demo();
//bind()函数会修改demo函数,将demo里边的this改为bind函数参数指定的对象
//并将新生成的demo函数返回,而原demo函数并不做修改。
var d = demo.bind({a:1,b:2});
var c = demo;
c();
d();
//output
/* Window {window: Window, self: Window, document: document, name: "", location: Location, …}
Window {window: Window, self: Window, document: document, name: "", location: Location, …}
{a: 1, b: 2} */
</script>
</body>
</html>
javascript函数的bind()函数作用
最新推荐文章于 2022-11-24 22:23:21 发布