webveuje/js/lx/a.html
2021-03-23 10:58:10 +08:00

92 lines
2.3 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 name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var a = 2
// var obj1 = {
// a: 1,
// fn1: (function (a) {
// this.a = a
// a++
// return function () {
// this.a = a++
// console.log(this)
// console.log(a)
// }
// })(a)
// }
// var fn1 = obj1.fn1
// var fn1=function () {
// this.a = a++
// console.log(this)
// console.log(a)
// }
// console.log(fn1.toString())
// fn1()
// console.log(getA)
// if ('a' in window) {
// var a = ''
// function getA(a) {
// a = a || this.a
// console.log(this.a)
// }
// getA(a)
// }
var c = 3
function getC() {
this.c++
console.log(this,'kkkk')
return function () {
c = this.c * 2
console.log(c)
}
}
var obj3 = {
c: 2,
getC: (function () {
this.c -= 1 //win.c-1 win.c=2
console.log(this.c, this,'sss')
return this.getC //obj3.getc=win.getc
})()
}
// var a=obj3.getC()
// a()
// obj3.getC()()
getC() //win.c++ =>win.c=3
console.log(obj3.getC.toString(),"aaaaaaa")
obj3.getC()
// console.log(obj3.c,window.c)
var f3=obj3.getC;
f3()
// console.log(window.c)
// console.log(obj3.c)
//step1 立即执行函数 => this 指向window win.c-1 -> win.c=2
//step2 getC() this.c -=> win.c++ =>2+1 =>3
//step3 obj3.getC() step1中 obj3.getc= win.getc 所以就是执行win.getc
// 即 winc++ win.c=3
//obj3.getc时 this指向的是obj3this.c操作的是obj3.c , 所以obj3.c 也+1等于3
//step4 f3本身等于win.getc函数 执行f3() 即执行 win.getc() =>win.c++ win.c=4
</script>
</head>
<body>
</body>
</html>