<!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>
<button>点击</button>
<script>
// 函数定义的方式
// 1. 自定义函数
// function fn() {}
// 2. 构造函数
// var f = new Function(参数,参数3,函数题)
// 3. 函数表达式
// var fun = function()
//1.普通函数 this->window
function fun() {
}
fun()
//2.对像方法 this->对像
var b = {
s: function() {
}
}
b.s()
//3.构造函数 this->实例对象 构造函数原型对象的this也是实例对象
function S() {
}
new S()
//4.绑定事件函数 构造函数 this->实例对象
// 触发事件调用
//5. 定时器函数 this->window
// 6.立即执行函数,自动调用 this->window
(function() {
})()
//改变this指向方法 call apply bind
// call(对象,参数1,参数2....) 函数.call(对像) 主要作用实现属性继承
// apply(对像,[]) 1.调用函数2.改变this指向 参数必须为数组 主要用作数组
// apply() 的主要作用var max =Math.max.apply(Math, array)
// bind:绑定函数,只会改变this指向,不会调用,只是调用函数,参数与call同,返回原函数this改变后的新韩淑 主要用作:定时器
var array = [1, 3, 3, 100, 2, 1, 11, 1, ]
var max = Math.max.apply(Math, array)
console.log(max)
//_________________________________________________________________________
// bind绑定后, 函数不会立即执行, 举例子
var btn = document.querySelector("button")
btn.onclick = function() {
this.disabled = true;
setTimeout(function() {
this.disabled = false
}.bind(this), 3000)
}
//严格模式下 'use strict'
// 区别:1.变量名先声明后使用2.不可以随意删除已经声明的变量3.严格模式下this指向underfunde
//4.以前构造函数时不加new也可以调用,当普通函数,this指向全局 严格模式下不加new,this报错 5.不允许函数名重名
// __________________________________________________________________________________________________
// 闭包:指有权去访问别的函数作用域中变量的函数.被访问的变量的所在函数就是闭包函数,
// 也就是一个作用域可以访问另外一个函数的局部变量
function fn() {
var num = 10;
function fun() {
console.log(num)
}
}
// 如何实现fn外访问num?
function fn() {
var num = 10;
function fun() {
console.log(num)
}
return fun
}
var f = fn()
f()
// 类似f= function fun() {
// console.log(num)
// }
</script>
</body>
</html>