JavaScript 火狐window.event为空的替代方式

<!DOCTYPE html>
<html>
<head>
<meta charset="utf8">
<script>
//严格模式下,这些都是不能用的
//TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
//'use strict'
function fun(args){
	console.log( 'arguments instanceof Array? ' + (arguments instanceof Array) );//false
    console.log( 'arguments instanceof Object? ' + (arguments instanceof Object) );//true
	//arguments是一个map类型的参数对象(即使参数列表为空,也会返回一个length=0的map对象)
	console.log(arguments);
	//arguments的值虽然与arguments.callee.arguments相同,但是它两却不相等,这说明它们之间是深拷贝
	console.log("arguments==arguments.callee.arguments : " + arguments==arguments.callee.arguments);
	//argument.callee就是当前函数本身
	console.log(arguments.callee);
	console.log(arguments.callee.arguments);
	//argument.callee.caller是调用当前函数的函数(如果调用者不是函数,或者说是位于JS顶层,那么该值为null)
	console.log(arguments.callee.caller);
	if(arguments.callee.caller != null) {
		console.log("start print event......");
		console.log(window.event);
		//arguments.callee.caller.arguments存储的是事件而不是参数列表(如果是自定义的一般函数,这里是undefined)
		console.log(arguments.callee.caller.arguments);
		const _e = window.event || arguments.callee.caller.arguments[0]; //由于火狐window.event为空,只能使用该方法兼容火狐
		console.log(_e);
	}
	//打印收到的第一个参数
    console.log(args);
	console.log("==========================")
}
fun("noclick", 1,2,3)

function delegateFun() {
	fun("delegate", 1,2,3)
}
delegateFun();
</script>
</head>
<body>
<button onclick="fun('click', 1,2,3)">Test</button>
</body>
</html>

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值