ES 6個人メモ——対象拡張


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);
2.Object.is()
ES5           "==" "==="
           
  NaN   NaN,  +0  -0

ES6         
console.log(Object.is(+0,-0),(+0 === -0),Object.is(NaN,NaN),(NaN === NaN));
3.Object.assign()
                     
PS:
	1.           (        )
	2.           (emumerable:false)
	3.       
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));
    :
	1.       
	2.       
	3.    
	4.      
	5.        
// 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
//             
const merge1 = (target,...sources) => Object.assign(target,...sources);
//               ,       ,        
const merge2 = (...sources) => Object.assign({},...sources);
     enumerable     “      ”
      false,               。

ESS 3       enumerable false   。
	1.for ... in  :                  。 
	2.Object. keys ():                  。 
	3.JSON. stringify ():               。
5.属性の巡回
for...in
Object.keys(obj)
Object.getOwnPropertyNames(obj)
Object.getOwnPropertySymbols(obj)
Reflect.ownKeys(obj)
6.__プロト.属性
// ES5
let obj = Object.create(someOtherObj);
obj.method = function(){...};
// ES6
let obj = {
	method : function(){...};
};
obj.__proto__ = someOtherObj;

// Object.setPrototypeOf()
//          prototype  ,        
//  ES6              
//   
// Object.setPrototypeOf(Object,prototype);
//   
// 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(entry);
}

// entries  
// 1.       
// 2.        Map  
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.getOwn PropertyDescriptors()
// ES5:             (descriptor)
console.log("ES5",Object.getOwnPropertyDescriptor(obj2,'foo'));
// ES6:            (     )     
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';