如何在 JavaScript 中检查变量或对象的类型?

WBOY
WBOY 转载
2023-09-03 10:09:02 876浏览

如何在 JavaScript 中检查变量或对象的类型?

JavaScript 是一种松散类型的编程语言,这意味着没有这样的规则来声明变量类型。一个变量可以在程序中存储多种数据类型,因此在使用变量之前了解变量类型非常重要。在 JavaScript 中,我们可以使用 typeof 运算符来检查变量或对象的类型。 typeof 运算符接受一个变量并以字符串格式返回其类型。

除了typeof运算符之外,JavaScript还提供了instanceof运算符来检查变量或对象的类型。 instanceof 运算符接受两个参数:要检查的对象和要检查的类型的构造函数。如果构造函数是对象类型,则该运算符将返回 true。

使用 typeof 运算符

typeof 运算符是一种一元运算符,它接受一个参数并返回一个指示参数类型的字符串。例如,typeof 运算符可用于检查变量或对象的类型。

语法

typeof variable_name 

在上面的语法中,variable_name 是要确定其类型的变量的名称。

typeof 运算符可以返回以下字符串之一 -

  • “number”代表数字

  • “string”表示字符串

  • 布尔值的“boolean”

  • “未定义”表示未定义的值

  • “object”表示对象(包括数组和函数)

  • 符号的“symbol”(ECMAScript 2015 中的新增功能)

示例

在此示例中,我们使用 typeof 运算符来检查 JavaScript 中变量或对象的类型。我们声明了不同类型的多个变量,例如数字、字符串、布尔值等。我们在网页上显示了这些变量。我们在按钮上使用了单击事件处理程序来检查变量的类型。用户只要点击该按钮,就可以在网页上看到所有变量及其类型。 typeof 运算符有助于在执行特定操作之前确定变量或对象的类型。例如,您可以使用它来确保在执行算术之前变量是数字,或者在将变量与另一个字符串连接之前确保变量是字符串。

<html>
<body>
   <h2>Checking the <i> type of a variable or object </i> in JavaScript</h2>
   <h4>The variables are as follows:</h4>
   <ul>
      <li>let num = 10</li>
      <li>let str = "Hello"</li>
      <li>let bool = true</li>
      <li>let un</li>
      <li>let n = null</li>
      <li>let arr = [1, 2, 3]</li>
      <li>let func = function () {}</li>
   </ul>
   <button onclick = "checkType()"> Check Type </button>
   <div id = "root"> </div>
   <script>
      let num = 10
      let str = 'Hello'
      let bool = true
      let un
      let n = null
      let arr = [1, 2, 3]
      let func = function () {}
      let root = document.getElementById('root')
      function checkType() { 
         root.innerHTML = '<h4>The types of variables are as follows:</h4>'
         root.innerHTML += '<ul> <li> let num = 10 is a ' + typeof num + ' </li> <li> let str = "Hello" is a ' + typeof str + ' </li> <li> let bool = true is a ' + typeof bool + ' </li> <li> let un is a ' + typeof un + ' </li> <li> let n = null is a ' + typeof n + ' </li> <li> let arr = [1, 2, 3] is a ' + typeof arr + ' </li> <li> let func = function () {} is a ' + typeof func + ' </li> </ul> '
      }
   </script>
</body>
</html> 

使用instanceof运算符

在 JavaScript 中,instanceof 运算符用于在运行时确定对象的类型。它返回一个布尔结果,指示该对象是否是特定类的实例。

语法

object_name instanceof object_constructor 

在上面的语法中,object_name 是您要确定其类型的对象的名称。

示例

在此示例中,我们使用 instanceof 运算符来检查 JavaScript 中变量或对象的类型。我们用 String 类构造函数和自定义类对象“myClassObject”声明一个字符串类型变量,“myClassObject”是“myClass”的对象,并将它们显示在网页上。我们在按钮上使用单击事件处理程序来检查对象的类型并将其显示在网页上。

<html>
<body>
   <h2>Checking the <i> type of a variable or object </i> in JavaScript</h2>
   <h4>The object variables are as follows:</h4>
   <ul> 
      <li>let str = new String('Hello World!')</li>
      <li>let myClassObject = new MyClass()</li>
   </ul>
   <button onclick = "checkType()"> Check Type </button>
   <div id = "root"> </div>
   <script>
      let str = new String('Hello World!')
      class MyClass {}
      let myClassObject = new MyClass()
      let root = document.getElementById('root')
      function checkType() {
         root.innerHTML = '<h4> The types of objects using instanceof operator: </h4>'
         root.innerHTML += '<ul> <li> str is an instance of String: ' + (str instanceof String) + ' </li> <li> str is an instance of MyClass: ' + (str instanceof MyClass) + ' </li> </ul>'
         root.innerHTML += ' <ul> <li> myClassObject is an instance of String: ' + (myClassObject instanceof String) + ' </li> <li> myClassObject is an instance of MyClass: ' + (myClassObject instanceof MyClass) + ' </li> </ul>'
      }
   </script>
</body>
</html> 

typeof 和 instanceof 运算符仅在与某些对象一起使用时有时才会返回预期结果。例如,typeof 运算符为数组返回“object”,即使它们是 JavaScript 中的一种对象类型。要正确检查值是否为数组,可以使用 Array.isArray() 方法。

以上就是如何在 JavaScript 中检查变量或对象的类型?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除