简体   繁体   English

打字稿在速记构造函数中创建对象

[英]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.而是传入xy的值,但仍然保持使用速记符号来保存我完整地写出类。

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属性的值,其x1y2 But it exposes an optional fourth constructor parameter that overrides the x and y properties:但它公开了一个可选的第四个构造函数参数,该参数覆盖了xy属性:

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.使用普通方法。

Playground link to code Playground 链接到代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Typescript 参数作为 object 传递时的构造函数简写 - Typescript constructor shorthand when parameters are passed as an object Typescript object 可选的初始值设定项错误,默认属性在构造函数简写中声明 - Typescript object initializer error for optional with default property declared in constructor shorthand Typescript编译器中的对象简写语法 - object shorthand syntax in typescript compiler 在typescript中绑定接口构造函数有什么简写吗? - Is there any shorthand for binding interface constructor in typescript? TypeScript中的object参数映射到另一个object有简写吗? - Is there a shorthand for mapping object parameters to another object in TypeScript? 在TypeScript中,创建具有http构造函数的相同类的对象 - In TypeScript, create object of same class having constructor of http 打字稿构造函数新对象 - typescript constructor new object TypeScript - 将一个对象的所有属性分配给另一个对象的速记方式 - TypeScript - Shorthand way to assign all properties of one object to another 如何使用内部特定操作创建 useDispatch 的简写? Typescript - Howt to create a shorthand for useDispatch with a specific action inside? Typescript Vue.js - Typescript:对象文字中的预期方法简写 - Vue.js - Typescript: Expected method shorthand in object literal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM