Files
ClassContent/历届学生/front-end-team-10/项目练习/学生项目/韩孟雪/2023-3-4/this指向.html
2024-09-27 02:06:13 +08:00

124 lines
2.7 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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>this指向</title>
</head>
<body>
</body>
<script>
//function () {}
//创建类
// class swiper {
// time = 200
// text (){
// var time = 100;
// console.log("test :",this.time );
// }
// }
// var a = new swiper()
// a.text()
// console.log(a.time);
//创建对象
// var time = 400
// var a = {
// time : 200,
// test : () => {
// var time = 3000
// console.log(this.time);
// }
// }
// console.log(a.test());
// var pw=111
// function a(){
// var user = "123",pw=333
// console.log(this.pw)
// console.log(this);
// }
// a();
// var o = {
// user :"123"
// fn:function (){
// console.log(this.user);
// console.log(this);
// }
// }
// o.fn();
// var o = {
// a:10,
// b:{
// fn:function(){
// console.log(this.a);
// }
// },
// fn1:function(){
// console.log(this.a);
// }
// }
// o.fn1();
//选择最近的调用
// o.b.fn()
// function fn(){
// console.log(this);
// //this = fn()
// this.user = '123'
// return this;
// }
// var a = new fn;
// console.log(a.user);
// var o = {
// a: 10,
// b: {
// a: 12,
// fn: function () {
// console.log(this.a); //undefined
// console.log(this); //window
// }
// }
// }
// var j = o.b.fn;//没加()赋值给j,this调用时才指向具体函数
// j();//this指向windows
// var time = 4000
// var a = {
// time : 2000,
// b:function(){
// this.time = 5000;
// var test = () => {
// console.log(this.time);
// }
// return test
// }
// }
//箭头函数没有自己的this 内部的this值以外部非箭头函数的this
//没有function 不能使用new
// var ddd = new a.b()
// ddd()
//箭头函数 和 普通函数的区别
//如何改变this 指向?
var name = '小王',age = '17';
var obj = {
name : '小张',
objAge:this.age,
myFunction(){
console.log(this.name + "年龄"+this.age);
}
}
obj.objAge;
obj.myFunction()
</script>
</html>