// this -- 谁调用指向谁
/**
* 构造函数: new Vue()
* this指向的是 实例化的对象
*
*/
// function Animal(name,age) {
// this.name = name
// this.age = age
//
// console.log(this);
// }
//
// var a = new Animal('小狗',10)
//
// console.log(a);
/**
* 全局中指向 window
*/
// console.log(this);
/**
* 函数中
* 谁最后调用指向谁
*/
// function fn1() {
// console.log(this);
// }
//
// // fn1()
//
// let obj = {
// a:1,
// fn: fn1
// }
//
// obj.fn() // this指向obj
// let a = obj.fn
// a() // 指向的是window
/**
* 箭头函数不会改变this指向
*/
// let arr = [1,2,3,]
//
// arr.forEach(()=>{
// console.log(this);
// })
/**
* 常用的方法
* methods: {
* getData(){
* let vm = this
*
* }
* }
*/
/**
* 强制改变 this指向
*
* call
* bind 改变 this指向 后 需要调用函数
* apply
*/
关于this 指向
最新推荐文章于 2024-05-21 19:09:41 发布