JS函数
JS中函数定义的三种方式
- 直接通过function关键字定义函数
- 使用函数表达式定义匿名函数
- 使用new Function构造函数定义函数
// JS中函数的定义有三种方式
// 1.直接通过function定义函数(命名函数)
function fun() {
}
// 2.使用函数表达式定义匿名函数
var fun2 = function () {
};
console.dir(fun2);
console.log("==================");
// 3.使用Function构造函数定义函数
var f = new Function("a", "b", "console.log(a + b)")
f(1, 2);
// 4.所有的函数都是Function实例对象
// f是Function的一个实例对象,所有拥有Function的原型对象__proto__,该原型对象中的构造方法指向Function的构造方法
// f本身也是一个函数,所有拥有一个原型对象prototype,该原型对象中的构造方法指向f的构造方法
console.dir(f); //
// 5.函数也属于对象
console.log(f instanceof Object);
注:
1.所有的函数都是Function实例对象
2.函数也属于对象
函数中this的指向
函数不同的调用方式决定了函数的中this指向的不同
- 普通函数中的this指向window
- 对象中的方法,this指向的是对象
- 构造函数中的this和构造函数原型对象中的this都指向的是这个实例对象
- 绑定事件函数 this 指向的是函数的调用者 btn这样的按钮控件对象
- 定时器函数,this指向Window
- 立即执行函数,this指向的还是window
// 函数的不同调用方式决定了this 的指向不同
// 1. 普通函数 this 指向window
function fn() {
console.log('普通函数的this' + this);
}
window.fn();
// 2. 对象的方法 this指向的是对象 o
var o = {
sayHi: function() {
console.log('对象方法的this:' + this);
}
};
o.sayHi();
// 3. 构造函数 this 指向 ldh 这个实例对象 原型对象里面的this 指向的也是 ldh这个实例对象
function Star() {};
Star.prototype.sing = function() {
};
var ldh = new Star();
// 4. 绑定事件函数 this 指向的是函数的调用者 btn这个按钮对象
var btn = document.querySelector('button');
btn.onclick = function() {
console.log('绑定时间函数的this:' + this);
};
// 5. 定时器函数 this 指向的也是window
window.setTimeout(function() {
console.log('定时器的this:' + this);
}, 1000);
// 6. 立即执行函数 this还是指向window
(function() {
console.log('立即执行函数的this' + this);
})();
总结
调用方式 | this指向 |
---|---|
普通函数function | window |
定时器函数 | window |
立即执行函数 | window |
构造函数 | 实例对象 |
构造函数中的原型对象 | 实例对象 |
绑定事件函数 | 绑定的控件对象(如btn按钮) |
改变函数中this指向的三种方法
call()
- 调用函数,改变函数中的This指向
- 实现继承
// 改变函数内this指向 js提供了三种方法 call() apply() bind()
// 1. call()
var o = {
name: 'andy'
};
function fn(a, b) {
console.log(this);
console.log(a + b);
};
fn.call(o, 1, 2);
// call 第一个可以调用函数 第二个可以改变函数内的this 指向
// call 的主要作用可以实现继承
function Father(uname, age, sex) {
this.uname = uname;
this.age = age;
this.sex = sex;
}
function Son(uname, age, sex) {
Father.call(this, uname, age, sex);
}
var son = new Son('刘德华', 18, '男');
console.log(son);
apply()
- 和Call方法一样,apply方法也会立刻执行函数,并且改变函数内部的this指向
- 不同点:apply的函数参数必须是一个数组!!!
// 改变函数内this指向 js提供了三种方法 call() apply() bind()
// 2. apply() 应用 运用的意思
var o = {
name: 'andy'
};
function fn(arr) {
console.log(this); // o对象
console.log(arr); // 'pink'
}
fn.apply(o, ['pink']);
// 1. 也是调用函数 第二个可以改变函数内部的this指向
// 2. 但是他的参数必须是数组(伪数组)
// 3. apply 的主要应用 比如说我们可以利用 apply 借助于数学内置对象求数组最大值
// Math.max();
var arr = [1, 66, 3, 99, 4];
var arr1 = ['red', 'pink'];
// var max = Math.max.apply(null, arr);
var max = Math.max.apply(Math, arr);
var min = Math.min.apply(Math, arr);
console.log(max, min);
bind
- bind函数不会调用原来的函数,仅仅是改变原来函数中的this指向。
- 它的本质是原函数改变this之后所产生的新函数
- 如果有的函数我们不需要立即调用,但是又想改变这个函数内部的this指向时可以使用bind方法
// 改变函数内this指向 js提供了三种方法 call() apply() bind()
// 3. bind() 绑定 捆绑的意思
var o = {
name: 'andy'
};
function fn(a, b) {
console.log(this);
console.log(a + b);
}
var f = fn.bind(o, 1, 2);
f();
// 1. 不会调用原来的函数 可以改变原来函数内部的this 指向
// 2. 返回的是原函数改变this之后产生的新函数
// 3. 如果有的函数我们不需要立即调用,但是又想改变这个函数内部的this指向此时用bind
// 4. 我们有一个按钮,当我们点击了之后,就禁用这个按钮,3秒钟之后开启这个按钮
// var btn1 = document.querySelector('button');
// btn1.onclick = function() {
// this.disabled = true; // 这个this 指向的是 btn 这个按钮
// // var that = this;
// setTimeout(function() {
// // that.disabled = false; // 定时器函数里面的this 指向的是window
// this.disabled = false; // 此时定时器函数里面的this 指向的是btn
// }.bind(this), 3000); // 这个this 指向的是btn 这个对象
// }
var btns = document.querySelectorAll('button');
for (var i = 0; i < btns.length; i++) {
btns[i].onclick = function() {
this.disabled = true;
setTimeout(function() {
this.disabled = false;
}.bind(this), 2000);
}
}
JS严格模式
JS开启严格模式的两种方式
- 为整个脚本开启严格模式
- 为某个函数开启严格模式
<!-- 为整个脚本(script标签)开启严格模式 -->
<script>
'use strict';
// 下面的js 代码就会按照严格模式执行代码
</script>
<script>
(function() {
'use strict';
})();
</script>
<!-- 为某个函数开启严格模式 -->
<script>
// 此时只是给fn函数开启严格模式
function fn() {
'use strict';
// 下面的代码按照严格模式执行
}
function fun() {
// 里面的还是按照普通模式执行
}
</script>
严格模式下的规范
- 变量必须先声明,再使用
- 不能随意删除已经声明的变量
- 严格模式下,全局作用域中的this是undefined
- 严格模式下,如果构造函数不加new调用,this指向的是undefined,如果这时使用this.xxx = xxx 复制会报错。
- 定时器中的this还是指向的window
- 严格模式下的参数不允许有重名
'use strict';
// 1. 我们的变量名必须先声明再使用
// num = 10;
// console.log(num);
var num = 10;
console.log(num);
// 2.我们不能随意删除已经声明好的变量
// delete num;
// 3. 严格模式下全局作用域中函数中的 this 是 undefined。
// function fn() {
// console.log(this); // undefined。
// }
// fn();
// 4. 严格模式下,如果 构造函数不加new调用, this 指向的是undefined 如果给他赋值则 会报错.
// function Star() {
// this.sex = '男';
// }
// // Star();
// var ldh = new Star();
// console.log(ldh.sex);
// 5. 定时器 this 还是指向 window
// setTimeout(function() {
// console.log(this);
// }, 2000);
// a = 1;
// a = 2;
// 6. 严格模式下函数里面的参数不允许有重名
// function fn(a, a) {
// console.log(a + a);
// };
// fn(1, 2);
function fn() {}