创建直接的实例
person=new Object();
person.firstname="John";
person.lastname="Doe";
person.age=50;
person.eyecolor="blue";
替换语法:
person={firstname:"John",lastname:"Doe",age:50,eyecolor:"blue"};
使用对象构造器
function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
}
JavaScript 基于 prototype,而不是基于类的。
把方法添加到 JavaScript 对象
<script>
function person(firstname,lastname,age,eyecolor){
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
this.changeName=changeName;
function changeName(name){
this.lastname=name;
}
}
myMother=new person("Sally","Rally",48,"green");
myMother.changeName("Doe");
document.write(myMother.lastname);
</script>
JavaScript for...in 循环
var person={fname:"John",lname:"Doe",age:25};
for (x in person)
{
txt=txt + person[x];
}
所有 JavaScript 数字均为 64 位
精度:整数(不使用小数点或指数计数法)最多为 15 位;小数的最大位数是 17,但是浮点运算并不总是 100% 准确。
var myNumber=128;
myNumber.toString(16); // 返回 80
myNumber.toString(8); // 返回 200
myNumber.toString(2); // 返回 10000000
无穷大(Infinity)
NaN - 非数字值
数字可以是数字或者对象
var x = 123;
var y = new Number(123);
typeof(x) // 返回 Number
typeof(y) // 返回 Object
在一个数组中你可以有不同的对象
把属性添加到 JavaScript 对象
person.firstname="John";
person.lastname="Doe";
person.age=30;
person.eyecolor="blue";
x=person.firstname;