1. js的变量类型只有var运算符一种
ex:
var x = 2;var str = "string"
js中数据有两种类型:1.数值类型 2.文本类型
在向变量赋值时,数值类型的变量不需要加引号,反之则按照文本数据类型处理.在声明变量时,加入未使用值来声明的变量,其值实际上是undefined.
2. JavaScript拥有动态类型特点
相同的变量可用作不同的类型数据.
3. JavaScript 对象
js中所有的事物都是对象:字符串,数字,数组,日期等等
对象是拥有属性和方法的数据
创建JavaScript对象
ex:
person = new Object();
person.firsrname="Bill";
person.lastname="Gates";
person.age=56;
person.eyecolor="blue";
本例创建名为"person"的对象,并为其添加了四个属性.
js:
function createObjectExample(){
person = new Object();
person.firstname = "Bill";
person.lastname = "Gates";
person.age = 56;
person.eyecolor = "blue";
document.write(person.firstname + "is" + person.age + "years old.");
}
html:
<!--创建对象例子-->
<p></p>
<button type="button" οnclick="createObjectExample()">Create Object</button>