![]() ![]() | |
Flash Player 5。
newconstructor()
constructor 一个函数,其后括号中可以是任何可选参数。此函数通常是要构造的对象类型的名称(如 Array、Number 或 Object)。
无。
运算符;创建一个初始时匿名的新对象,然后调用由 constructor 参数标识的函数。new 运算符将括号中的任何可选参数和用关键字 this 引用的新建对象传递给函数。然后构造函数可以用 this 来设置对象的变量。
下面的示例创建 Book() 函数,然后使用 new 运算符来创建对象 book1 和 book2。
function Book(name, price){
this.name = name;
this.price = price;
}
book1 = new Book("Confederacy of Dunces", 19.95);
book2 = new Book("The Floating Opera", 10.95);
下面的示例使用 new 运算符来创建具有 18 个元素的 Array 对象:
golfCourse_array = new Array(18);
![]() ![]() | |