[英]How can i access JavaScript constructor object as an IFFI function parameter?
[英]How can I call a javascript/typescript constructor by passing an object as parameter?
如果我使用以下代碼調用它,我應該如何編寫我的構造函數
const person = new Person({
name,
gender
})
其中名稱和性別都是字符串
更新:
這就是我在 typescript 中得到的:
interface Details {
name: string;
gender: string;
}
class Person {
name: string;
gender: string;
constructor({ name, gender }: Details) {
this.name = name;
this.gender = gender;
}
}
const person = new Person({
name: "John",
gender: "male",
});
console.log(person);
工作代碼!!
在JavaScript
class Person { constructor({name, gender}) { this.name = name; this.gender = gender; } } const name = 'John'; const gender = 'male'; const person = new Person({ name, gender }); console.log(person);
在TypeScript
interface Details { name: string; gender: string; } class Person implements Details { constructor({ name, gender }: Details) { this.name = name; this.gender = gender; } } const name = 'John'; const gender = 'male'; const person = new Person({ name, gender }); console.log(person);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.