四个 this 的典型应用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m_review/article/details/90903586

构造函数中

function Animal(name, age) {
  this.name = name;
  this.age = age;
}

绑定 DOM 元素

<input type="button" onclick="showInfo(this);" value="button" />

获取 DOM 元素的属性或者值

<input id="btn" type="button" value="input" />;
let btn = document.getElementById("btn");
btn.onclick = function() {
  console.log(this.value);
};

call apply 求取数组最大值

let arr = [3, 2, 5, 1, 6];
Math.max.apply(this, arr);
Math.max.call(this, 3, 2, 5, 1, 6);

猜你喜欢

转载自blog.csdn.net/m_review/article/details/90903586