JavaScript TIL 06
8893 ワード
2021年6月29日に書かれた書類です.
JavaScriptの勉強内容を整理しました.
オブジェクトはキー値ペアで構成されます. キー: 値: と価値は のカッコ キー値の対は
1. Dot notation
このエラーは、定義されていない変数
したがって、
Written with StackEdit .
JavaScriptの勉強内容を整理しました.
オブジェクト
オブジェクトの宣言
let user = {
firstName : 'John'
lastName : 'Lee'
email : '[email protected]';
city : 'Seoul';
}
firstName
、lastName
、email
、city
'John'
、'Lee'
、'[email protected]'
、'Seoul'
:
に分かれています.{}
を使用してオブジェクトを作成します.,
に分割される.オブジェクト値を使用する2つの方法
1. Dot notation
let user = {
firstName : 'John'
lastName : 'Lee'
email : '[email protected]';
city : 'Seoul';
}
user.firstName; // 'John'
user.city; // 'Seoul'
2. Bracket notationlet user = {
firstName : 'John'
lastName : 'Lee'
email : '[email protected]';
city : 'Seoul';
}
user['firstName']; // 'John'
user['city']; // 'Seoul'
しかしここにuser[city]
と書くとエラーが発生します.このエラーは、定義されていない変数
city
を参照して発生します.したがって、
[]
に文字列ではなく変数を書き込む場合は、オブジェクト内のキーがフローティングである場合に書き込むことが望ましい.オブジェクトとして追加できる
1.deleteで削除
let user = {
firstName : 'John'
lastName : 'Lee'
email : '[email protected]';
city : 'Seoul';
}
delete user.city; // city 키-값 쌍을 지운다.
delete user.city;
でcity
キー値ペアをクリアすると、オブジェクトは次のようになります.let user = {
firstName : 'John'
lastName : 'Lee'
email : '[email protected]';
}
city
キー-値ペアがクリアされていることがわかります.2.Dot記号とBracket記号を使用して値を追加
let user = {
firstName : 'John'
lastName : 'Lee'
email : '[email protected]';
city : 'Seoul';
}
user.category = 'hello'; // Dot notation 써서 값 추가
user['otherThing'] = 'hi'; // Bracket notation 써서 값 추가
Dot notation
、Bracket notation
を使用して以下のように値上げします.let user = {
firstName : 'John'
lastName : 'Lee'
email : '[email protected]';
city : 'Seoul';
category = 'hello';
otherThing = 'hi';
}
city
キーの下にいくつかの値が追加されていることがわかります.3.in演算子を使用して、対応するキーがあるかどうかを確認します。
let user = {
firstName : 'John'
lastName : 'Lee'
email : '[email protected]';
city : 'Seoul';
}
'lastName' in user; // true
'content' in user; // false
対応する鍵がある場合はtrue
、対応する鍵がない場合はfalse
を返します.Written with StackEdit .
Reference
この問題について(JavaScript TIL 06), 我々は、より多くの情報をここで見つけました https://velog.io/@heewonkim-dev/JavaScript-TIL-06テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol