[英]Typescript create object in shorthand constructor
I have a Foo and a Bar class:我有一个 Foo 和一个 Bar 类:
class Foo {
private name: string;
private bar: Bar;
constructor(name: string, bar: Bar) {
this.name = name;
this.bar = bar;
}
}
class Bar {
private x: number;
private y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
}
Now using the Typescript constructor shorthand I could write Foo as:现在使用 Typescript 构造函数简写,我可以将 Foo 写为:
class Foo {
constructor(private name: string, private bar: Bar) { }
}
However what I would like to do, is instead of passing a Bar
object into the Foo
constructor.但是我想做的是,而不是将
Bar
对象传递给Foo
构造函数。 Is instead pass in the values of x
and y
, but still maintain using the shorthand notation to save me writing out the class in full.而是传入
x
和y
的值,但仍然保持使用速记符号来保存我完整地写出类。
Effectively, can this:有效地,这可以:
class Foo {
private name: string;
private bar: Bar;
constructor(name: string, x: number, y: number) {
this.name = name;
this.bar = new Bar(x, y);
}
}
Be written making use of the shorthand notation?使用速记符号编写?
The shorthand notation is called "parameter properties" , and specifically just copies a constructor parameter to a same-named class property.简写符号称为“参数属性” ,特别是将构造函数参数复制到同名类属性。 It isn't suitable for what you're doing with
bar
.它不适合你正在做的事情
bar
。 You can still use the shorthand for the name
property, like this:您仍然可以使用
name
属性的简写,如下所示:
class Foo {
private bar: Bar;
constructor(private name: string, x: number, y: number) {
this.bar = new Bar(x, y);
}
}
which is how I'd recommend you proceed.这就是我建议你继续的方式。
If someone were to threaten me with bodily harm unless I came up with some way to use parameter properties for your bar
scenario, I suppose I could abuse default parameters like this:如果有人用身体伤害威胁我,除非我想出某种方法来为您的
bar
场景使用参数属性,我想我可以像这样滥用默认参数:
// ☠ DON'T DO THIS ☠
class AbuseFoo {
constructor(
private name: string,
x: number, y: number,
private bar: Bar = new Bar(x, y)
) { }
}
This "works" in that这在那个“工作”
// ☠ DON'T DO THIS ☠
let f = new AbuseFoo("name", 1, 2);
console.log(JSON.stringify(f)); // {"name":"name","bar":{"x":1,"y":2}}
produces a value containing a private bar
property whose x
is 1
and y
is 2
.生成一个包含私有
bar
属性的值,其x
为1
, y
为2
。 But it exposes an optional fourth constructor parameter that overrides the x
and y
properties:但它公开了一个可选的第四个构造函数参数,该参数覆盖了
x
和y
属性:
f = new AbuseFoo("name", 1, 2, new Bar(3, 4));
console.log(JSON.stringify(f)); // {"name":"name","bar":{"x":3,"y":4}}
And of course, it's ugly and more complicated than just declaring bar
in the class and setting it in the constructor the normal way.当然,它比在类中声明
bar
并以正常方式在构造函数中设置它丑陋且复杂。 So unless you have some very good reason to do this (eg, code obfuscation contest, threats of bodily harm, etc), I'd stick with the original answer:因此,除非您有充分的理由这样做(例如,代码混淆竞赛、人身伤害威胁等),否则我会坚持原始答案:
No, you can't use parameter properties to set bar
that way.不,您不能使用参数属性以这种方式设置
bar
。 Use the longhand method.使用普通方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.