Javascript如何知道变量的类型? [英] How does Javascript know what type a variable is?
问题描述
我不知道为什么我从未问过自己过去几年的质疑,但突然间我找不到自己或谷歌的任何答案。
I don't know why I never asked myself that questioned the last years before, but suddenly I could not find any answer for myself or with google.
Javascript众所周知,变量没有类型。真是件好事。
但不知何故,它必须确定类型并使用它。
Javascript is known to have no types for variables. A really nice thing. But somehow it must determine the type and work with it.
var a = 1;
var b = 2.0;
var c = 'c';
var d = "Hello World!";
所以我们有一个整数,双/浮点数,字符,字符串(可能会被拆除) as char *)
So what we have is an Integer, Double/Float, Character, String (which may be teared down as char*)
我知道JS使用运行时解释器,但是想到逻辑和类型必须以任何方式实现..
I know that JS works with a runtime interpreter, but thinking of that the logic and "type" must be implemented in any way..
那么Javascript Interpreter如何识别并在内部处理变量?
在我的想象中,假设我会编写C ++,我会想到一种模板和容器以及一些逻辑,它会重载运算符并尝试检查它到底是什么。但这并没有想到最后。
So how does a Javascript Interpreter recognize and internally handle the variables? In my imagination, assuming I would write C++, I would think of a sort of template and container and a bit of a logic that overloads operators and try to check, what it really is. But that's not thought to the end.
请与我分享你的知识: - )
Share your knowledge with me please :-)
推荐答案
JavaScript根据值赋值设置变量类型。例如,当JavaScript遇到以下代码时,它知道 myVariable
应该是类型编号:
JavaScript sets the variable type based on the value assignment. For example when JavaScript encounters the following code it knows that myVariable
should be of type number:
var myVariable = 10;
同样,JavaScript将在以下示例中检测到变量类型是字符串:
Similarly, JavaScript will detect in the following example that the variable type is string:
var myVariable = "Hello World!";
JavaScript比许多其他编程语言灵活得多。对于Java等语言,必须在创建变量时将变量声明为特定类型,并且一旦创建,就不能更改该类型。这被称为 强打字 。另一方面,JavaScript允许随时更改变量的类型,只需指定不同类型的值(更好地称为 松散的打字 )。
JavaScript is also much more flexible than many other programming languages. With languages such as Java a variable must be declared to be a particular type when it is created and once created, the type cannot be changed. This is referred to as strong typing. JavaScript, on the other hand, allows the type of a variable to be changed at any time simply by assigning a value of a different type (better known as loose typing).
以下示例完全有效地使用了JavaScript中的变量。在创建时,变量显然是类型编号。稍后将字符串赋值给此变量会将类型从数字更改为字符串。
The following example is perfectly valid use of a variable in JavaScript. At creation time, the variable is clearly of type number. A later assignment of a string to this variable changes the type from number to string.
var myVariable = 10;
myVariable = "This is now a string type variable";
变量的数据类型是JavaScript脚本引擎的解释类型变量当前持有的数据。字符串变量包含字符串;数字变量包含数字值,依此类推。但是,与许多其他语言不同,在JavaScript中,同一个变量可以包含不同类型的数据,所有这些都在同一个应用程序中。这是松散类型和动态类型这两个术语所知的概念,这两个概念都意味着JavaScript变量可以根据上下文在不同时间保存不同的数据类型。
The variable’s data type is the JavaScript scripting engine’s interpretation of the type of data that variable is currently holding. A string variable holds a string; a number variable holds a number value, and so on. However, unlike many other languages, in JavaScript, the same variable can hold different types of data, all within the same application. This is a concept known by the terms loose typing and dynamic typing, both of which mean that a JavaScript variable can hold different data types at different times depending on context.
完整文章: http://www.techotopia.com/index.php/JavaScript_Variable_Types
Complete article here: http://www.techotopia.com/index.php/JavaScript_Variable_Types
另一篇文章可以帮助您: http://oreilly.com/javascript/excerpts/learning-javascript/javascript-datatypes-variables.html
Another Article Which may help you: http://oreilly.com/javascript/excerpts/learning-javascript/javascript-datatypes-variables.html
有用的链接:
ECMAScript Language Specification
ECMAScript BNF Grammar
JAVAScript BNF Gramar
这篇关于Javascript如何知道变量的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!