LoginSignup
0
0

More than 3 years have passed since last update.

オブジェクトにプロパティを追加する

Posted at

オブジェクトにプロパティを追加する

Q.オブジェクトってなに?

→JavaScriptにおけるオブジェクトとは、データ(プロパティ)や機能(メソッド)をまとめた情報の集合体のこと

Q.プロパティってなに?

→ハッシュ形式で書かれている値(データ)のこと

Q.メソッドってなに?

→プロパティに紐づけられた処理のこと
「〇〇をしてほしい」という処理を、関数を使って表す

【具体例】

let human = { 
  name: "Jhon"

  talk: function(){
    console.log(`私の名前は${human.name}です`)
  }
}

human.talk()

human → オブジェクト
name: "Jhon” → プロパティ
talk → メソッド

オブジェクトにプロパティを追加する方法

方法は2種類ある

  • オブジェクト.プロパティ = 値 で追加する
  • オブジェクト['プロパティ'] = 値 で追加する

【具体例】

let human = { name: 'nataka' }

human.age = 20
human['address'] = 'Tokyo'

console.log(human)
//{name: "tanaka", age: 20, address: "Tokyo"}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0