<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
/* var person = {
firstName: "gao",
lastName: "xing",
id: 123,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};
console.log(person.fullName());*/
/*
JavaScript this关键词指的是他所属的对象
它拥有不同的值,具体取决于它的使用位置:
在方法中,this指的是所有者对象
单独的情况下,this指的是全局对象(普通情况下和严格模式下都是)
在函数中,this指的是全局对象
在函数中,严格模式下,this是undefined
在事件中,this指的是接收事件的元素
像call()和apply()这样的方法可以将this引用到任何对象
*/
/*
方法中的this
在对象方法中,this指的是此方法的拥有者。
在上面的实例中,this指的是person对象
person对象是fullName方法的拥有者。
*/
/*
单独的this(普通情况下和严格模式下都是)
在单独使用时,拥有这是全局对象,因此this指的是全局对象
在浏览器窗口中,全局对象时[window object]
*/
/* "use strict";
var x = this;
console.log(x);*/
/*
函数中的this(默认)
在javaScript函数中,函数的拥有着默认绑定this
因此,在函数中,this指的是全局对象[object window]
*/
/* function myFunction(){
return this;
}
console.log(myFunction());*/
/*
函数中的this(严格模式)
JavaScript严格模式下不允许默认绑定
因此,在函数中使用时,在严格模式下,this是未定义的(undefined)????为什么结果不是undefined 在使用严格模式的时候必须在<script>的头部进行声明
*/
/* "use strict";
function myFunction2(){
console.log(this);
}
myFunction2();*/
/*
事件处理程序中的this
在HTML事件处理程序中,this指的是接受此事件的HTML元素
*/
/*<button onclick = "this.style.display = 'none'"> 点击删除 </button>*/
</script>
<button onclick = "this.style.display = 'none'"> 点击删除 </button>
</body>
</html>
关于call()和apply()的应用可以在我的博客中找到相关的介绍