我想创建一个将其他对象作为属性(例如position
)的复合对象(例如device
)。 这可能通过使用构造函数在JavaScript中实现吗?JavaScript - 具有对象属性的复合对象
var pos = new position(23,10,15);
var dev = new device(01,"mobile",pos);
//device obj constructor
function device(id, type, position) {
this.id=id;
this.type=type;
this.position = {position.lat, position.lon, position.alt};
}
//position obj constructor
function position(lat,lon,alt) {
this.lat=lat;
this.lon=lon;
this.alt=alt;
}
我收到错误“SyntaxError:Unexpected token。”。
你需要使用伪经典方法? (换句话说,你需要使用构造函数吗?) –
如果可能的话,我会更喜欢这种方式。 – LePhleg