对象构造器
本例使用函数来构造对象:
function person(firstname, lastname, age, eyecolor)
{
this.firstname = firstname;
this.lastname = lastname;
this.age = age;
this.eyecolor = eyecolor;
}在JavaScript中,this通常指向的是我们正在执行的函数本身,或者是指向该函数所属的对象(运行时)
function movie(title, director) {
this.title = ;
this.director =;
}
创建 JavaScript 对象实例
一旦您有了对象构造器,就可以创建新的对象实例,就像这样:
var myFather = new person("John","Doe",50,"blue");
var myMother = new person("Sally","Rally",48,"green");
document.write(myFather.age); // -> 50
document.write(myMother.name); // -> Sally提示: myFather 和 myMother 是 person 对象的实例,它们的属性分配给相应的值。
创建对象
请考虑以下示例。
function person (name, age) {
this.name = name;
this.age = age;
}
var John = new person("John", 25);
var Loen = new person("Loen", 28);使用点语法访问对象的属性。