Javascript变量类型
Javascript variable types
你们能解释一下下面两个变量的区别吗?
var test = {};
var test2 = [];
它们不是都声明了一个数组变量吗?如果不是,什么时候用哪个?
第一个创建一个对象(没有任何属性的空对象)。
第二个函数创建一个空数组。
让我们举一个操作对象的例子:
var test = { firstName: 'Foo', lastName: 'Bar' };
alert(test.firstName);
您还可以动态扩展现有的空对象并为其添加属性:
var test = { };
test.firstName = 'Foo'; // or an equivalent: test['firstName'] = 'Foo';
alert(test.firstName);
和数组:
var test2 = [ { firstName: 'Foo', lastName: 'Bar' },
{ firstName: 'Foo 2', lastName: 'Bar 2' } ];
for (var i = 0; i < test2.length; i++) {
alert(test2[i].firstName);
}
或向数组中添加元素:
var test = { firstName: 'Foo', lastName: 'Bar' };
var test2 = [ ];
test2.push(test); // the array contains 1 element now
alert(test2[0].firstName);
第一个变量test
是一个对象,有变量keys
和values
,第二个变量test2
是一个数组,有固定的keys
(0,1,2,3,…)
var test = ['a', 'b', 'c'];
alert(test[0]); // alerts 'a'
var test2 = {
first: 'a',
second: 'b',
third: 'c'
};
alert(test2.first); // alerts 'a'
alert(test2['first']); // alerts 'a'
第一个是对象表示法,第二个是数组对象(它本身就是一个对象)。
可以在对象中存储关联数据,但数组键只能是数字。
这里我提供了一个代码链接,我准备用例子和细节来解释你:JavaScript变量声明和类型,点击链接阅读代码,自己测试并给出一个喜欢。
https://code.sololearn.com/Wef3g7HLH5SM/?ref=app下面是代码:
<!--
In JavaScript you don not have to declare explicitly the type of variables, however JavaScript variables
can hold many data types: numbers, strings, objects. Below I am going to provide you couple examples,
I hope they help you!
-->
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
// Single line comment
/*
Multi-lines comment
*/
/*
The typeof operator can return one of these primitive types:
* string
* number
* boolean
* null
* undefined
*/
var person = undefined; // a variable without a value, has the value 'undefined'
var vehicle = ""; // The value is "", the typeof is "string"
/*
null type, is "nothing". It is supposed to be something that doesn't exist.
*/
var house = null; // Value is null, but type is still an object
var number = 1;
/*
JavaScript has dynamic types. Meaning that
you can use the same variable to hold different data types.
*/
var x; // Now x is undefined
var x = 6; // Now x is a Number
var x = "Sam"; // Now x is a String
/*
Numbers can be written with, or without decimals places
*/
var x1 = 2.50; // Written with decimals
var x2 = 5; // Written without decimals
/*
Extra large or extra small numbers can be written with
scientific (exponential) notation
*/
var a = 123e4; // will print: 1230000
var b = 123e-4; // will print: 0.0123
/*
Booleans can only have two values: true or false.
*/
var y = true;
var z = false;
/*
Single or double quotes can be use to write Strings.
*/
var stringVariable1 = "This is my name: 'Sam'"; // Using single quotes
var stringVariable2 = 'Here, my name: "Sam"'; // Using double quotes
/*
JavaScript arrays are written with square brackets.
Array items are separated by commas.
*/
var girls = ["Julia", "Mary", "Olivia"];
/*
JavaScript objects are written with curly braces.
Object properties are written as name: value pairs, separated by commas.
*/
var userData = {firstName:"John", lastName:"Doe", userName:"JDoe",
password:123456};
document.getElementById("demo").innerHTML =
typeof person + "<br>" +
typeof vehicle + "<br>" +
typeof number + "<br>" +
typeof y + "<br>" +
typeof house + "<br>" +
number + "<br>" +
x + "<br>" +
x1 + "<br>" +
x2 + "<br>" +
a + "<br>" +
b + "<br>" +
y + "<br>" +
z + "<br>" +
stringVariable1 + "<br>" +
stringVariable2 + "<br>" +
// here I am going to print the value on array, index 0
girls[0] + "<br>" +
userData.firstName + "'s userID: " + userData.userName +
" and pwd: " + userData.password;
</script>
</body>
</html>
相关文章:
- javascript解释器如何理解变量的数据类型
- Javascript如何知道变量的类型
- Jquery变量类型问题
- 变量类型错误
- 没有从本地存储获取与保存的变量相同的类型
- 在 Javascript 中创建全局变量时是否需要指定对象类型
- 想要将密码类型存储在 JavaScript 变量中
- 当类型存储在变量中时,不使用eval创建对象的实例
- 如何从html输入类型=“”中只获得Angular控制器变量中的时间值;时间”;
- 将变量从FancyBox传递到父级时发生未捕获的类型错误
- 怎么了?j ?类型J:变量J=9;
- 特殊的JavaScript结构:对象定义中的对象类型变量
- (JS)为什么两个日期类型变量共享相同的内容
- 字符串:javascript中的基本类型变量或引用类型变量
- 如何通过JSON在MySQL中插入Date类型变量
- 在js中分离单个var类型变量
- 隐式类型变量的javascript问题
- 如何通过ajax更新js对象类型变量
- 在打字稿中对联合类型变量使用非共享方法
- 对象类型变量的闭包