본문 바로가기
IT/JavaScript

ES5 문법 #4 ~ JSON, Date, Object...

by 2T1 2022. 12. 15.
 

ES5 문법 #3 ~ Array

2022.12.13 - [IT/JavaScript] - ES5 문법 #1 2022.12.13 - [ALL] - ES5 문법 #2 ~ string Array.isArray() (객체 판단) var names = ["James", "Jone", "Daniel"] var obj = {name : "M.Lee", age : 23} Array.isArray(names) // return true Array.isArray(obj) //r

who-is-2t1.tistory.com


JSON.parse() / JSON.stringify()

JSON 데이터를 obj로 변환

var obj = JSON.parse('{"name" : "M.Lee", "age" : 26}');

obj를 JSON 형태로 변환

var obj = {name : "M.Lee", age: 25};
var strJSON = JSON.stringify(obj);

 

Date.now()

var tms1 = Date.now();  // return 1670918169619
var tms2 = showTime();  // return 1670918169619

function showTime(){
	var d = new Date();
	return d.getTime();
}

Date.now()는 Date 객체에서 getTIme()을 한 결과값과 동일한 값을 반환한다.

 

Date toISOString()

Date Obj를 ISO 표준 형식(ISO 8601)을 사용하여 문자열로 변환시킨다.

const dts = new Date();
var d = dts.toISOString();  // d = 2022-12-13T10:23:08.653Z //UTC 시간대

 

Date toJSON()

Date값을 JSON으로 직렬화 할 때 유용하게 사용할 수 있다.

const dts = new Date();
var d = dts.toJSON();  // d = 2022-12-13T10:24:28.817Z

 

Property Getters and Setters

var product = {
	type : "Apple",
	color : "Red",
	get fullInfo() {
		return this.color + " " + this.type;
	},
	set colour(val){
		this.color = val;
	}
}
console.log(product.fullInfo);  // return "Red Apple"
product.colour = "Green"
console.log(product.fullInfo);  // return "Green Apple"

 

Object.defineProperty()

//Object.defineProperty() is a new Object method in ES5.
//Object.defineProperty(obj, prop, descriptor)
// obj : 속성을 정의할 객체
// prop : 새로 정의하거나 수정하려는 속성의 이름 또는 Symbol
// descriptor : 새로 정의하거나 수정하려는 속성을 기술하는 객체

// Create an Object:
var person = {
  firstName: "John",
  lastName : "Doe",
  language : "NO"
};

// Change a Property:
Object.defineProperty(person, "language", {
  get : function() { return language },
  set : function(value) { language = value.toUpperCase()}
});

// Change Language
person.language = "en";

// Display Language

value 또는 writable을 / get 또는 set과 함께 가지고 있으면 오류가 발생

 

// configurable : false

속성 값을 변경할 수 있고, 객체에서 삭제할 수 있을때 true 로 사용

 

// enumerable : false

속성이 객체의 속성 열거 시 노출되면 true 로 사용

 

// value : 'undefined'

속성에 연관된 값.

 

// writable : false

할당 연산자로 속성의 값을 바꿀 수 있으면 true

 

// get : 'undefined'

속성의 접근자로 사용할 함수. 접근할 때 사용한 객체를 이 함수의 this로 설정. 매개변수 없이 호출한 뒤 그 반환 값을 이 속성의 값으로 취급

 

// set : 'undefined'

속성의 설정자로 사용할 함수. 이 속성에 값을 할당하면, 할당할 때 사용한 객체를 이 함수의 this로 설정하고, 한 개의 매개변수로 호출

 

Object.defineProperty() - JavaScript | MDN

Object.defineProperty() 정적 메서드는 객체에 새로운 속성을 직접 정의하거나 이미 존재하는 속성을 수정한 후, 해당 객체를 반환합니다.

developer.mozilla.org

 

E5 Object Methods

 

JavaScript ES5 Object Methods

W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

www.w3schools.com

 

Function Bind()

bind를 이용하여 개체가 다른 개체에서 method를 빌릴 수 있음

const fruit = {
	name : 'Apple',
	price : 800,
	tag : function(){
		return this.name + ' : ' + this.price + '원';
	}
}

const menu = {
	name : 'Ramen',
	price : 3000
}

let mainMenu = fruit.tag.bind(menu);

 

 

 

JavaScript Function bind() Method

W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

www.w3schools.com

 

Trailing Commas

const fruit = {
	name : 'Apple',  // O
	price : 800,     // O
}

const menu = {
	name : 'Ramen',   // O
	price : 3000      // O
}

//JSON (Objects, Arrays both) does not allow trailing commas.

'IT > JavaScript' 카테고리의 다른 글

Vite #1  (0) 2022.12.26
JavaScript 번들러  (0) 2022.12.26
ES5 문법 #3 ~ Array  (0) 2022.12.14
ES5 문법 #2 ~ string  (0) 2022.12.13
ES5 문법 #1  (0) 2022.12.13