var o = {
'a': 3, 'b': 4,
'doStuff': function() {
alert(this.a + this.b);
}
};
o.doStuff(); // displays: 7
你可以做一个结构工厂。
function makeStruct(names) {
var names = names.split(' ');
var count = names.length;
function constructor() {
for (var i = 0; i < count; i++) {
this[names[i]] = arguments[i];
}
}
return constructor;
}
var Item = makeStruct("id speaker country");
var row = new Item(1, 'john', 'au');
alert(row.speaker); // displays: john
export class Point {
public readonly X: number;
public readonly Y: number;
constructor(x: number, y: number)
{
this.X = x;
this.Y = y;
}
public static SetX(value: number) : Point {
return new Point(value, this.Y);
}
public static SetY(value: number) : Point {
return new Point(this.X, value);
}
}