JavaScript对象属性(二)

对象object 

例子一:

var car = {
"wheels":4,
"engines":1,
"seats":5
};

例子二:

var Car = function() {
this.wheels = 4;
this.engines = 1;
this.seats = 1;
};//构造函数

调用构造函数

var Car = function() {
this.wheels = 4;
this.engines = 1;
this.seats = 1;
};

var myCar=new Car();

myCar.nickname="aaaa";

注意:当 myCar (即 Car 的一个 实例 )创建后,他可以像普通对象一样被使用,包括创建、访问、修改它的属性等,就像我们使用其他对象一样。

向构造函数中添加参数 

var Car = function(wheels,seats,engines) {
//Change this constructor
this.wheels =wheels;
this.seats = seats;
this.engines = engines;
};

posted on 2016-12-03 17:26  onPemako  阅读(103)  评论(0编辑  收藏  举报