如何在 JavaScript 函数中使用 typeof 参数?
Arguments 对象是传递给函数的参数。它是一个所有函数都可访问的变量。假设向函数传递了两个参数,那么你可以像下面这样访问它们
arguments[0] arguments[1]
以相同的方式,你可以在 JavaScript 中使用 typeof 参数。首先,让我们看看如何使用 typeof。typeof 运算符是一个单目运算符,它放在其单个操作数之前,该操作数可以是任何类型。
示例
以下代码演示了如何实现 typeof 运算符
<html>
<body>
<script>
var a = 20;
var b = "String";
var linebreak = "<br />";
result = (typeof b == "string" ? "B is String" : "B is Numeric");
document.write("Result => ");
document.write(result);
document.write(linebreak);
result = (typeof a == "string" ? "A is String" : "A is Numeric");
document.write("Result => ");
document.write(result);
document.write(linebreak);
</script>
</body>
</html>现在让我们看看如何在 JavaScript 中使用 typeof 参数。typeof 参数会返回一个类似这样的对象
document.write(typeof arguments);
假设你有两个参数,那么使用 typeof,你可以像下面这样引用它们,它将返回 typeof 参数。
document.write(typeof arguments[0]); document.write(typeof arguments[1]);
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP