JavaScript 是否可以创建新的数据类型

JavaScript 是否可以创建新的数据类型

本文中我们将要完成的任务是在JavaScript中是否可以创建新的数据类型。例如,让我们考虑一个变量student –

var student = "username"
console.log(typeof student)

现在,我想声明变量的类型并将其打印到输出中,它将如下所示 –

Output:"string"

让我们深入文章,了解如何在JavaScript中创建一种新的数据类型。

是的,可以在JavaScript中创建一种新的数据类型,可以使用Class的概念。如果你想要检查实际的数据类型,可以使用 instanceof运算符。

JavaScript中的类用于构建模型。代码用于封装和操作数据。JS中的类基于原型,但也包括一些与ES5类不同的语法和语义。

instanceof运算符告诉数据类型。以下是一个简单的JavaScript代码示例,它将简要介绍如何创建一种新的数据类型以及如何检查数据类型。这里,我将提供自定义实现来检查数据类型。

为了更好地理解如何在JavaScript中创建一种新的数据类型,让我们看一下以下示例。

示例

在下面的示例中,我们使用 instanceof 运算符来检查函数的类型。

<!DOCTYPE html>
<html>
   <body>
      <script>
         class car {
            constructor(color, old) {
               this.color = color;
               this.old = old;
            }
         }
         const value = new car("value", 400);
         function checkType(data) {
            if (data instanceof car) return "car";
            return typeof data;
         }
         document.write(checkType(value) + " < br > ");
         document.write(checkType("value") + " < br > ");
      </script>
   </body>
</html>

当脚本被执行时,它将生成一个输出,其中包含在执行脚本时触发的事件所显示的网页上的函数类型。

示例

考虑以下示例,我们在这里使用对象类型来创建一个新的数据类型,并且还可以在JavaScript中检测数据类型。

<!DOCTYPE html>
<html>
   <body>
      <script>
         function Student() {}
         const student = new Student()
         const getObjTag = (obj) => Object.prototype.toString.call(obj).slice(8, -1)
         document.write(getObjTag([]) + " < br > ")
         document.write(getObjTag(new Set([])))
      </script>
   </body>
</html>

在运行上述脚本时,将弹出输出窗口,显示运行脚本时触发的事件获得的网页上的数据类型。

示例

让我们来看一下下面的脚本,其中我们运行脚本以获取JavaScript中的数据类型。

<!DOCTYPE html>
<html>
   <body>
      <script>
      class Car {
         constructor(color, model, price) {
            this._color = color;
            this._model = model;
            this._price = price;
         }
         info() {
            return `The color of the car is {this._color} and, its model is{this._model}. It costs arround ${this._price}`
         };
      };
      const car = new Car("black", 2022, "4cr");
      document.write(car.info(), " < br > ")
      document.write("Type of the object: ", typeof car)
      </script>
   </body>
</html>

当脚本执行时,事件被触发并在网页上显示文本以及数据类型。

示例

执行以下操作以观察如何在JavaScript中创建新的数据类型。

<!DOCTYPE html>
<html>
   <body>
      <script>
         class Game {
            constructor(gameName) {
               this.gameName = gameName;
            }
         }
         const ticTacToe = new Game("TicTacToe");
         function dataTypeBelongsTo(object) {
            if (object instanceof Game) return "Game";
            return typeof object;
         }
         document.write("The ticTacToe is the object of Game class: " + (ticTacToe instanceof Game) + " < br > ");
         document.write("The data Type of ticTacToe is: " + dataTypeBelongsTo(ticTacToe) + " < br > ");
         document.write("The data Type Candy Cash is: " + dataTypeBelongsTo("Cady Cash"))
      </script>
   </body>
</html>

运行以上脚本时,将弹出输出窗口,触发事件并在网页上显示数据类型。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程