美文网首页
JS中的this

JS中的this

作者: wyc0859 | 来源:发表于2022-03-10 01:54 被阅读0次
const age = 18;
function myFunction(a, b) {
  return a * b;
}
const res1 = myFunction(10, 2);
console.log("res1:", res1); // 20

以上函数不属于任何对象。但是在JS中,始终存在一种默认的全局对象。
在 HTML 中,默认全局对象是 HTML 页面本身,所有上面的函数 属于 HTML 页面。
在浏览器中,这个页面对象(window)就是浏览器窗口 ,上面的函数自动成为一个窗口函数

console.log("window:", window); 

注意上面这行代码:window等于浏览器窗口,所以在终端运行这里是会报错的

this的最终指向,是那个调用它的对象

函数(对象方法)中的this

function funa() {
  const funName = "函数A";
  //console.log("函数下window等于this吗?", window === this); //true
  console.log("函数下this:", this); //浏览器下是window对象,终端下是Object [global]
  console.log("this.funName:", this.funName, funName); //this.funName: undefined 函数A
//之所以是undfined ,是因为this的对象是全局对象window,
//而funName不是全局变量,因此window无法获取到funName
}
funa(); 

浏览器执行了funa()函数,this就是window
终端执行了funa()函数,this就是Object [global]

console.log("this:", this); //this: {}
console.log("this.age:", this.age); //this.age: undefined
//console.log("window等于this吗?", window === this); //false

直接console.log("this:", this); 会返回{}
如上面 执行了funa()函数,是浏览器执行的funa(),this就是window
但就这样放个this在外部,什么都没执行,this哪里来的调用它的对象,所以是{}


const obj = {
  name: "我是对象",
  say: function () {
    console.log("obj对象的say方法", this); // {say: ƒ say()} 
  },
};
obj.say();

上面代码,this的最终指向,是那个调用它的对象,很明显是obj


常见的情况

for循环时,常用 x = obj[key] x()操作。但此时的x()this是window需注意

var testobj = {
  age: 23,
  eat: function () {
    console.log("this:", this);
    console.log("年龄:", this.age);
  },
};
function prt(obj) {
  testobj.eat(); //this是testobj,有年龄
  obj["eat"](); //this是testobj,有年龄
  const x = obj["eat"];
  x(); //this是window,年龄是undefined
}
prt(testobj);
外部直接用也是一样
testobj.eat(); //this是testobj,有年龄
testobj["eat"](); //this是testobj,有年龄
const x = testobj["eat"];
const y = testobj.eat;
x(); //this是window,年龄是undefined
y(); //this是window,年龄是undefined

构造函数下的this

const objc = new func("参数");
func构造函数体内部的this是objc,不是func

function func(param) {
  funName = "变量"; //这个不是属性
  this.funName = "实例属性"; //这是构造函数属性

  console.log("func:", this, this.prototype); //明显this是objc
  //func  {funName: '实例属性'} undefined
  //this.prototype为undefined,证明不是构造函数,而是实例对象

  //这个不是方法,仅普通函数
  const c1 = function () {
    console.log("普通函数:", this);
  };
  //这个才是构造函数的方法
  this.c2 = function () {
    console.log("构造函数的方法:", this); //this是objc
  };
  this.c3 = () => {
    console.log("构造函数的方法-箭头函数:", this); //this是objc
  };
}

const objc = new func("参数"); 
//new一个对象,底层分3步,了解的话,就知道内部this一定是objc了
console.log("objc.funName:", objc.funName); //实例属性
console.log("func.funName:", func.funName); //undefined,这是调用静态

// objc.c1(); //会报错
objc.c2(); //func {funName: '实例属性', c2: ƒ c3: ƒ}
objc.c3(); //箭头函数:func {funName: '实例属性', c2: ƒ, c3: ƒ}

借用构造函数下的this

function Girl(height, bra) {
  this.height = height;
  this.bra = bra;
  console.log("Girl的this:", this); //young {height: 175, bra: 'C'}
  this.cry = function () {
    console.log(`${this.name}哭了`);
  };
}
Girl.prototype.smile = function () {
  console.log("她笑了");
};

function young(name, height, bra) {
//console.log("young:", young); //ƒ young(name, height, bra){} 函数体
//换成打印young(),会报错
  console.log("this:", this); //young {}
  Girl.apply(this, [height, bra]); // young借用Girl继承并调用了它
  this.name = name;
}

const xiaoB = new young("小b", 175, "C");
xiaoB.cry(); //小b哭了
console.log("xiaoB:", xiaoB);
//young {height: 175, bra: 'C', name: '小b', cry: ƒ}

//xiaoB.smile(); //报错
//注意:借用构造函数只是把参数传给了父类,但并不能访问父类的原型对象空间。
console.log("yong的原型对象空间:", young.prototype); 
//还是原有的 constructor仍指向自己

相关文章

  • JavaScript 04 (do...while循环/for

    js循环,js循环嵌套,js do...while循环,js的for循环,js中的break,js中的contin...

  • iOS原生&JS交互

    OC 获取js中的key OC调用js方法 JS调用OC中不带参数的block JS调用OC中带参数的block ...

  • JS 对象

    JS对象 JS对象的意义和声明 在JS中,对象(OBJECT)是JS语言的核心概念,也是最重要的数据类型。在JS中...

  • 单引号和双引号

    JS中 js中单引号和双引号的区别(html中属性规范是用双引号,js中字符串规定是用单引号)(js中单引号区别和...

  • js中的this

    一句话,call的第一个参数 看几个例子例1. 这里的this是什么?不要靠猜,是call的第一个参数,不知道去看...

  • js 中的this

    首先js中函数的this在函数被调用时总是指向一个对象(this对象是在运行时基于函数的执行环境绑定的) 然后 它...

  • JS中的this

    JS中的this 众所周知,JS中this的代表的是当前函数调用者的上下文。JS是解释性的动态类型语言,函数在调用...

  • js中的!!

    !!一般用来将后面的表达式强制转换为布尔类型的数据(boolean),也就是只能是true或者false。 var...

  • js中的this

    目标:js中this的指向? 问题的引出 指出this指向什么 js中函数的三种调用形式 func(p1, p2)...

  • JS中的this

    初学JavaScript经常被this绕晕,所以我总结一下JavaScript中的this。首先声明本文讨论的是非...

网友评论

      本文标题:JS中的this

      本文链接:https://www.haomeiwen.com/subject/vzzxdrtx.html