📑

オブジェクトについて

に公開

オブジェクトについて知らないことがあったのでまとめました

オブジェクトのメソッドを設定する方法

const object={
method:()=>{
console.log('Hello World');
}
}

object.method    // Hello World

省略可能なプロパティを設定

const object={
name:string,
password?:string
method?:()=>{
console.log('hello')
}
}
const information:object={
name:"Apple"
}

オブジェクトの分割代入

取り出したいプロパティ名を{}の中に列挙する

const object={
name:string,
email:string,
password:string,
}

const {name,password}=object

オブジェクトのプロパティ名とは異なる変数に代入したいときは
{プロパティ名:変数名,...}=オブジェクト; とする

const object={
name:string,
email:string,
password:string,
}

const {name:UserName,password:UserPassword}=object

入れ子構造の場合の分割代入

const object={
name:string,
email:string,
info:{
 color:string,
size:string,
}
}

const {email,info:{color}}=object

Discussion