简体   繁体   English

如何在javascript上设置默认数据类型?

[英]How can I set the default data type on javascript?

I wanna set the javascript data type on my vars. 我想在我的var上设置javascript数据类型。 ex: 例如:

var friends = {
  name: String(),
  age: Number(),
  classmate: Boolean()
};

Is this right? 这是正确的吗? It works on the browsers. 它可以在浏览器上使用。 but I can't be certain. 但我不能确定。 cause, below works too. 原因,下面也行。

var friends = {
  name: '',
  age: 0,
  classmate: false
};

I want to know if these things are right or not or good or bad. 我想知道这些事情是对还是错,还是好还是坏。

两者都是正确的,但是正确的方法是首先分配类型,在执行脚本和将值分配给变量的初始时间时将严格遵循。

Javascript is a weakly typed language, so this isn't doing anything for you. Javascript是一种弱类型语言,因此它对您没有任何帮助。 You do not need to declare the type of variables and you gain no protection by specifying a type when you initialize them. 您无需声明变量的类型,并且在初始化变量时通过指定类型不会获得任何保护。 If you need strong typing, I would consider TypeScript ; 如果您需要强类型,我会考虑使用TypeScript otherwise, I would enjoy not having to deal with them. 否则,我将不必与他们打交道。

First, javascript variables and properties are dynamic - they can take any values of any datatype. 首先,javascript变量和属性是动态的-它们可以采用任何数据类型的任何值。 What you are actually doing in your example is some sort of casting. 您在示例中实际要做的是某种类型的转换。

var friends = {
  index1: Number("12"),
  index2: String("12")
};

在此处输入图片说明

If it's a good or bad thing to initialize your variables such way really depends on your requirements. 初始化变量是好是坏,这种方式实际上取决于您的要求。 If you want to ensure that the value of each variable is consistent all through out the scope (and prevent any exceptions or unwanted results), then I guess it would be a good practice for you. 如果您想确保每个变量的值在整个范围内都是一致的(并防止出现任何异常或不良结果),那么我想这对您来说是个好习惯。

Take for instance. 举个例子。

var sum = friends.index1 + friends.index2;
console.log(sum);

The result is "1212" and not 24. Hopefully this makes sense to you. 结果是“ 1212”而不是24。希望这对您有意义。

There are other ways to ensure that the value is of type that you expected. 还有其他方法可以确保该值是您期望的类型。 You can check it using typeof. 您可以使用typeof进行检查。 Through this, you may no longer have to cast or "specify" the datatype during variable initialization. 这样,您就不必在变量初始化期间强制转换或“指定”数据类型。

if( typeof friends.index1 == "number" ) ...

In javascript values have a data type but variables don't. 在javascript中,值具有数据类型,但变量没有。 There is no mechanism to define the type of a variable and you can store any kind of value into an existing variable. 没有定义变量类型的机制,您可以将任何类型的值存储到现有变量中。 (In some languages this kind of weak typed variable is called a variable of variant type). (在某些语言中,这种弱类型变量称为变类型变量)。

For you information, ECMA script 2015 (ES6) introduced typed arrays that allow you to view a binary array buffer as an array of different length integer and floating point numbers. 仅供参考,ECMA脚本2015(ES6)引入了类型化数组 ,使您可以将二进制数组缓冲区作为不同长度的整数和浮点数的数组查看。 This is not specifically related to the wording of your question however, and is not the same as having typed variables. 但是,这与您的问题的措词没有特别的关系,并且与键入变量不同。

Note the first example in the post does not give variables a data type. 请注意,文章中的第一个示例并未为变量提供数据类型。 After

var friends = {
  name: String(),
  age: Number(),
  classmate: Boolean()
};

object property name contains a zero length string object, age contains 0 and classmate contains false, but you can reset them to anything afterwards. 对象属性name包含长度为零的字符串对象, age包含0, classmate包含false,但是之后可以将其重置为任何值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM