写出3个使用this的典型应用

(1)、在html元素事件属性中使用,如:

1
<input type=”button” onclick=”showInfo(this);” value=”点击一下” />

(2)、构造函数

1
2
3
4
function Animal(name, color) {
  this.name = name;
  this.color = color;
}

(3)、input点击,获取值

1
2
3
4
5
6
7
<input type="button" id="text" value="点击一下" />
<script type="text/javascript">
    var btn = document.getElementById("text");
    btn.onclick = function() {
        alert(this.value);    //此处的this是按钮元素
    }
</script>

(4)、apply()/call()求数组最值

1
2
3
4
5
var  numbers = [5, 458 , 120 , -215 ];
var  maxInNumbers = Math.max.apply(this, numbers); 
console.log(maxInNumbers);  // 458
var maxInNumbers = Math.max.call(this,5, 458 , 120 , -215);
console.log(maxInNumbers);  // 458

.

posted @   每天都要进步一点点  阅读(811)  评论(0编辑  收藏  举报
编辑推荐:
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
阅读排行:
· “你见过凌晨四点的洛杉矶吗?”--《我们为什么要睡觉》
· 编程神器Trae:当我用上后,才知道自己的创造力被低估了多少
· C# 从零开始使用Layui.Wpf库开发WPF客户端
· C#/.NET/.NET Core技术前沿周刊 | 第 31 期(2025年3.17-3.23)
· 接口重试的7种常用方案!
历史上的今天:
2018-03-06 vue 抽奖活动页 Lottery
2017-03-06 vue 开发中的常见问题
2017-03-06 vue-cli中配置sass
2017-03-06 解决 $ npm install node-sass --save-dev 失败的问题
点击右上角即可分享
微信分享提示