1. 程式人生 > >call與apply的作用---改變執行作用域

call與apply的作用---改變執行作用域

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body>
<script>
 window.color='red';
 var o = {
  color:'blue'
 }
 function sayColor (){
  console.log(this.color)
 }
 sayColor();//red
 sayColor.call(this);//red
 sayColor.call(window);//red
 sayColor.call(o);//blue
</script>
</body>
</html>