ES6 Personal Notes ノート - オブジェクトの拡張
2022-02-21 08:09:44
1.フェッチ機能、ストアバリュー機能
const obj = {
get foo(){},
set foo(x){}
}
// console.log(obj.foo.name);
const descriptor = Object.getOwnPropertyDescriptor(obj,'foo');
console.log(descriptor.get.name,descriptor.set.name);
ES5 compares equals using only two operators "==" and "==="
The former will automatically convert the data type
The latter NaN is not equal to NaN, and +0 is equal to -0
ES6 presents the same-value-equal algorithm
console.log(Object.is(+0,-0),(+0 === -0),Object.is(NaN,NaN),(NaN === NaN));
Used to copy all enumerable properties of the source object to the target object
PS:
1. only copy the source data's own properties (not including inherited properties)
2. Also do not copy non-enumerable properties (enumerable:false)
3. shallow copy is implemented
let target = {a : 1},
source1 = {b : 2},
source2 = {c : 3}
console.log(Object.assign(target,source1,source2),target);
let tar1 = {
a:{
b : 'c',
d : 'e'
}},
sou1 = {
a:{
b : 'hello'
}
};
console.info(Object.assign(tar1,sou1));
Common uses.
1. Adding properties to an object
2. Adding methods to an object
3. Cloning objects
4. Merge multiple objects
5. Specify default values for properties
// 1
class Point{
constructor(x,y){
Object.assign(this,{x,y});
}
}
// 2
Object.assign(SomeClass.prototype,{
someMethod(arg1,arg2){
...
},
anothorMethod(.. .args){
...
}
});
// 3
function clone(origin){
let originProto = Object.getPrototypeOf(origin);
return Object.assign(Object.create(originProto),origin);
}
// 4
// Merge multiple objects into one object
const merge1 = (target,... .sources) => Object.assign(target,... .sources);
// If you want the merge to return a new object, you can rewrite it to also lose, merge on an empty object
const merge2 = (... .sources) => Object.assign({},... .sources);
The enumerable property of the description object is called "enumerability".
If this attribute is false, it means that some operation will ignore the current attribute.
ESS has three operations that ignore properties with false enumerable.
1. for ... in loop: Iterates over only the object's own and inherited enumerable properties.
2.Object. keys (): Returns the key names of all enumerable properties of the object itself.
3. JSON. stringify (): Serialize only the object's own enumerable properties.
for... .in
Object.keys(obj)
Object.getOwnPropertyNames(obj)
Object.getOwnPropertySymbols(obj)
Reflect.ownKeys(obj)
// ES5
let obj = Object.create(someOtherObj);
obj.method = function(){...} ;
// ES6
let obj = {
method : function(){...} ;
};
obj.__proto__ = someOtherObj;
// Object.setPrototypeOf()
// Used to set the prototype object of an object, returning the parameter object itself
// is the officially recommended method for setting prototype objects in ES6
// Format
// Object.setPrototypeOf(Object,prototype);
// Usage
// let o = Object.setPrototype({},null);
// eg
let proto = {};
let obj = { x : 10 };
Object.setPrototypeOf(obj,proto);
proto.y = 20;
proto.z = 40;
console.log(obj.x,obj.y,obj.z,obj.__proto__);
let { keys,values,entries } = Object;
let obj1 = {'a' : 1,'b' : '3','c' : 2};
for(key of keys(obj1)){
console.log(key);
}
for(value of values(obj1)){
console.log(value);
}
for(entry of entries(obj1)){
console.log(entries);
}
// entries uses
// 1. Iterate through the object's properties
// 2. Turn the object into a real Map structure
let obj2 = { foo : 'bar',baz : 42 };
let map1 = new Map(entries(obj2));
console.log(map1)
let arr1 = [1,2,3,4,5];
for(let i in arr1){
console.log(arr1[i])
}
for(let i of arr1){
console.log(i)
}
// ES5: return the descriptor of an object's properties
console.log("ES5",Object.getOwnPropertyDescriptor(obj2,'foo'));
// ES6: return the description object for all own properties (not inherited properties) of the specified object
console.log("ES6",Object.getOwnPropertyDescriptors(obj2,));
const firstName = (message
&& message.body
&& message.body.user
&& message.body.user.firstName) || 'default';
const firstName = message?.body?.user?.firstName || 'default';
2.Object.is()の場合
ES5 compares equals using only two operators "==" and "==="
The former will automatically convert the data type
The latter NaN is not equal to NaN, and +0 is equal to -0
ES6 presents the same-value-equal algorithm
console.log(Object.is(+0,-0),(+0 === -0),Object.is(NaN,NaN),(NaN === NaN));
3.Object.assign()の場合
Used to copy all enumerable properties of the source object to the target object
PS:
1. only copy the source data's own properties (not including inherited properties)
2. Also do not copy non-enumerable properties (enumerable:false)
3. shallow copy is implemented
let target = {a : 1},
source1 = {b : 2},
source2 = {c : 3}
console.log(Object.assign(target,source1,source2),target);
4. オブジェクトのリファレンス
let tar1 = {
a:{
b : 'c',
d : 'e'
}},
sou1 = {
a:{
b : 'hello'
}
};
console.info(Object.assign(tar1,sou1));
Common uses.
1. Adding properties to an object
2. Adding methods to an object
3. Cloning objects
4. Merge multiple objects
5. Specify default values for properties
// 1
class Point{
constructor(x,y){
Object.assign(this,{x,y});
}
}
// 2
Object.assign(SomeClass.prototype,{
someMethod(arg1,arg2){
...
},
anothorMethod(.. .args){
...
}
});
// 3
function clone(origin){
let originProto = Object.getPrototypeOf(origin);
return Object.assign(Object.create(originProto),origin);
}
// 4
// Merge multiple objects into one object
const merge1 = (target,... .sources) => Object.assign(target,... .sources);
// If you want the merge to return a new object, you can rewrite it to also lose, merge on an empty object
const merge2 = (... .sources) => Object.assign({},... .sources);
The enumerable property of the description object is called "enumerability".
If this attribute is false, it means that some operation will ignore the current attribute.
ESS has three operations that ignore properties with false enumerable.
1. for ... in loop: Iterates over only the object's own and inherited enumerable properties.
2.Object. keys (): Returns the key names of all enumerable properties of the object itself.
3. JSON. stringify (): Serialize only the object's own enumerable properties.
5. 属性のトラバーサル
for... .in
Object.keys(obj)
Object.getOwnPropertyNames(obj)
Object.getOwnPropertySymbols(obj)
Reflect.ownKeys(obj)
6. proto__ 属性
// ES5
let obj = Object.create(someOtherObj);
obj.method = function(){...} ;
// ES6
let obj = {
method : function(){...} ;
};
obj.__proto__ = someOtherObj;
// Object.setPrototypeOf()
// Used to set the prototype object of an object, returning the parameter object itself
// is the officially recommended method for setting prototype objects in ES6
// Format
// Object.setPrototypeOf(Object,prototype);
// Usage
// let o = Object.setPrototype({},null);
// eg
let proto = {};
let obj = { x : 10 };
Object.setPrototypeOf(obj,proto);
proto.y = 20;
proto.z = 40;
console.log(obj.x,obj.y,obj.z,obj.__proto__);
7.Object.keys() Object.values() Object.entries()
let { keys,values,entries } = Object;
let obj1 = {'a' : 1,'b' : '3','c' : 2};
for(key of keys(obj1)){
console.log(key);
}
for(value of values(obj1)){
console.log(value);
}
for(entry of entries(obj1)){
console.log(entries);
}
// entries uses
// 1. Iterate through the object's properties
// 2. Turn the object into a real Map structure
let obj2 = { foo : 'bar',baz : 42 };
let map1 = new Map(entries(obj2));
console.log(map1)
let arr1 = [1,2,3,4,5];
for(let i in arr1){
console.log(arr1[i])
}
for(let i of arr1){
console.log(i)
}
8.Object.getOwnPropertyDescriptors()を使用する。
// ES5: return the descriptor of an object's properties
console.log("ES5",Object.getOwnPropertyDescriptor(obj2,'foo'));
// ES6: return the description object for all own properties (not inherited properties) of the specified object
console.log("ES6",Object.getOwnPropertyDescriptors(obj2,));
9. Null導体 ?
const firstName = (message
&& message.body
&& message.body.user
&& message.body.user.firstName) || 'default';
const firstName = message?.body?.user?.firstName || 'default';
関連
-
[解決済み] jQuery: Uncaught Error: 構文エラー、認識できない式
-
[解決済み] JavaScriptのオブジェクトを表示するにはどうすればよいですか?
-
[解決済み] モジュールが見つかりません。Error: Cannot resolve module
-
[解決済み] gulpタスク内のファイルを削除する
-
[解決済み] <td>と<tr>をクリックしたときのイベント
-
[解決済み] D3: 要素の削除
-
[解決済み] .jsm と .js ファイル
-
[解決済み] Gulp Watchが動作しない
-
[解決済み] JavaScriptのエラーです。Cannot read property 'includes' of undefined
-
[Turn] 基底パラメータradixがありません。
最新
-
nginxです。[emerg] 0.0.0.0:80 への bind() に失敗しました (98: アドレスは既に使用中です)
-
htmlページでギリシャ文字を使うには
-
ピュアhtml+cssでの要素読み込み効果
-
純粋なhtml + cssで五輪を実現するサンプルコード
-
ナビゲーションバー・ドロップダウンメニューのHTML+CSSサンプルコード
-
タイピング効果を実現するピュアhtml+css
-
htmlの選択ボックスのプレースホルダー作成に関する質問
-
html css3 伸縮しない 画像表示効果
-
トップナビゲーションバーメニュー作成用HTML+CSS
-
html+css 実装 サイバーパンク風ボタン
おすすめ
-
[解決済み] React with ES7: Uncaught TypeError: Cannot read property 'state' of undefined [duplicate] (未定義のプロパティ'state'を読み込むことはできません。
-
[解決済み】フォームが接続されていないため、フォーム送信がキャンセルされる Vue jsとLaravelの連携
-
[解決済み] JS - ReferenceError: fetchが定義されていません。
-
[解決済み] "ESLintの構成が見つかりません "エラー
-
[解決済み] jQuery 多次元配列の作成
-
[解決済み] ファイアストア 複数の条件付き where 節
-
[解決済み] 送信ボタンのonclickイベントで、フォーム送信をキャンセルするにはどうしたらいいですか?
-
[解決済み] ある値が数値の範囲内にあるかどうかを確認する
-
[解決済み] "React has detected a change in the order of Hooks" しかし、Hookは順番に呼び出されているようだ
-
.getAttribute is not a function error getAttributeについて少し考えてみました。