JavaScript Data Types
JavaScript has dynamic types, the same variable can be used as different types. JS有动态变量类型。
var x; // Now x is undefined
var x = 5; // Now x is a Number
var x = "John"; // Now x is a String
JavaScript Strings
字符串(text)用单引号或双引号围住。
You can use quotes inside a string, as long as they don't match the quotes surrounding the string
在字符串中可以嵌套使用单引号、双引号
var answer="It's alright";
var answer="He is called 'Johnny'";
var answer='He is called "Johnny"';
Or
you can put quotes inside a string by using the \ 使用斜杠屏蔽冲突引号
var answer='It\'s alright';
var carname="Volvo XC60";
var character=carname[7];
JavaScript Numbers
JavaScript has only one type of numbers. Numbers can be written with, or without decimals
JS只有一种数字类型,数字可以带小数,也可以没有小数点后面的部分。Extra large or extra small numbers can be written with scientific (exponential) notation
超大或超小的数字可以用科学指数法表示。
var y=123e5; // 12300000
var z=123e-5; // 0.00123
JavaScript Arrays
Array indexes are zero-based, which means the first item is [0]JS数组是以下标0开始的
JavaScript Objects
An object is delimited by curly braces. Inside the braces the object's properties are defined asname and value pairs (name : value) separated by commas。
JS对象由一对大括号定界的。对象的成员是由一对名字和值组成,由逗号分隔。
var person={firstname:"John", lastname:"Doe", id:5566};
Spaces and line breaks are not important 空格和分行不重要,因此也可以写为:var person={
firstname : "John",
lastname : "Doe",
id : 5566
};
Accessing Object Properties
两种方式引用对象的属性;
name=person.lastname;
name=person["lastname"];
Objects are just data, with added properties and methods. JS对象只是添加了属性和方法的数据
Properties are values associated with objects. Methods are actions objects can perform.
Declaring Variables as Objects
When a variable is declared with the keyword "new", the variable is declared as an object
使用关键字new来声明变量是一个对象,JS中任何变量都可以是对象
var name = new String;
var x = new Number;
var y = new Boolean;