ECMAScript ๊ณต๋ถ
1. ES5 ์ด์
1.1. string
1.2. ์ ๊ท ํํ์
2. ES5
2.1. ๋ฌธ๋ฒ ๋ณ๊ฒฝ ์ฌํญ
2.2. ์ฃผ์ ๋ณ๊ฒฝ ์ฌํญ
2.2.1. use strict
2.2.2. String.trim()
2.2.3. Array.isArray(object)
2.2.4. Array.forEach(func)
2.2.5. Array.map(func)
2.2.6. Array.filter(func)
2.2.7. Array.reduce(func)
2.2.8. Array.reduceRight(func)
2.2.9. Array.every(func)
2.2.10. Array.some(func)
2.2.11. Array.indexOf(item, [start])
2.2.12. Array.lastIndexOf(item, [start])
2.2.13. JSON.parse(string)
2.2.14. JSON.stringify(object)
2.2.15. Date.now()
2.2.16. Property Getters and Setters
2.2.17. New Object Property Methods
2.2.18. string ์์ฑ ์ ๊ทผ
3. ES6
3.0.1. let, const
3.0.2. arrow function
3.0.3. Loop Array, String
3.0.4. JS classes
3.0.5. JS Promises
3.0.6. Symbol type
3.0.7. default parameter values
3.0.8. rest parameter
3.0.9. Array.find(func)
3.0.10. Array.findIndex(func)
3.0.11. New Number Properties
3.0.12. New Number Method
3.0.13. New Global Methods
4. ES7
4.0.1. string.padStart(size, paddingChar)
4.0.2. string.padEnd(size, paddingChar)
ECMAScript ๊ณต๋ถ
1. ES5 ์ด์
- ES3 (1999) : ์ ๊ทํํ์, try/catch๋ฌธ
1.1. string
- string.length : ๊ธธ์ด ์์ฑ
- string.indexOf(string, [start]) : ํด๋น ๊ธ์์ ์์ index ์์น ๋ฐํ
- string.lastIndexOf(string, [start]) : ํด๋น ๊ธ์์ ๋ง์ง๋ง ์์ index ์์น ๋ฐํ
- string.search(string) : indexOf์ ๋น์ทํ์ง๋ง ์ ๊ทํํ์ ์ฌ์ฉ x
- string.slice(index, index2) : index๋ถํฐ index2-1๊น์ง ์๋ฅด๊ธฐ
- ์์ index๋ ๋ค์์๋ถํฐ ์
- string.substring(index, index2) : slice์ ๋น์ทํ์ง๋ง, ์์ ๋ถ๊ฐ
- string.substr(index, length) : index๋ถํฐ length๋งํผ ์๋ฅด๊ธฐ
- string.replace(string, string2) : string์ string2๋ก ๋ณํ
- string.toUpperCase() : ๋๋ฌธ์ ๋ง๋ค๊ธฐ
- string.toLowerCase() : ์๋ฌธ์ ๋ง๋ค๊ธฐ
- string.concat(string, string2) : string๊ณผ string2 ์ด์ด๋ถ์ฌ์ return
- string.split(๊ตฌ๋ถ์) : ๊ตฌ๋ถ์๋ก ๊ตฌ๋ถํ์ฌ array๋ก ๋ณํ
1.2. ์ ๊ท ํํ์
- ๋ฌธ๋ฒ
/pattern/modifier
pattern : ์ฐพ์์ผํ๋ ๊ฒ
modifier : case์ ๋ง์ถฐ ์ฐพ์ ์ ์๊ฒ ํด์ค
- pattern
- [abc] : ๋๊ดํธ ๋ด์ ์๋ฌด char์๋ ๋ง์ผ๋ฉด ๋จ
- [0-9] : ํด๋น ๋ฒ์ ๋ด
- (x|y) : ์ฌ๋ฌ ๊ฐ์ง ์ค ํ๋
- \d : digit ์ฐพ๊ธฐ
- \s : ๊ณต๋ฐฑ ์ฐพ๊ธฐ
- \b : ๋จ์ด ์์์ด๋ ๋
- \uxxxx : hex ์ซ์
- n+ : ์ ์ด๋ ํ๋์ n
- n* : 0์ด์
- n? : 0 ๋๋ 1๊ฐ
- modifier ์ข
๋ฅ
- i : ๊ธฐ๋ณธ
- g : ๋ชจ๋ matching ์ฐพ๊ธฐ
- m : multiline matching ์ฐพ๊ธฐ
- ํจ์
- pattern.test(string) ์ผ๋ก ํ
์คํธํด๋ณผ ์ ์์
- ์ฐพ์ผ๋ฉด true
- ์๋๋ฉด false
- pattern.exec(string) ์ผ๋ก ํ
์คํธํด๋ณผ ์ ์์
- ์ฐพ์ผ๋ฉด object
- ์๋๋ฉด null object
- pattern.test(string) ์ผ๋ก ํ
์คํธํด๋ณผ ์ ์์
2. ES5
2.1. ๋ฌธ๋ฒ ๋ณ๊ฒฝ ์ฌํญ
- []๋ก string value์ ์ ๊ทผ ๊ฐ๋ฅ
- array๋ {} ์ ๋ง์ง๋ง ์์์ ์ฝค๋ง
- {} : object literals
- ๋ง์ง๋ง ์์์ ์ฝค๋ง : Trailing comma
- ์ฃผ์ : JSON์๋ ํ์ฉ x, IE8์ crash๋จ
- string ์์์ multiline ๊ฐ๋ฅ
- ์์ฑ ์ด๋ฆ์ผ๋ก ์์ฝ์ด ์ฌ์ฉ ๊ฐ๋ฅ
2.2. ์ฃผ์ ๋ณ๊ฒฝ ์ฌํญ
2.2.1. use strict
use strict ๊ตฌ๋ฌธ์ด global ๋ณ์ ์ฒ๋ฆฌ๋์ด ํด๋น ๋ฒ์์ ์๋ js๊ฐ strict mode์์ ์คํ๋๋ค.
strict mode๋?
-
๋ณ์ ์ด๋ฆ์ ์๋ชป ๊ธฐ์ฌํ๋ฉด, ๋ค๋ฅธ ๋ณ์๋ฅผ ์์ฑํ๋ ๊ฒ์ด ์๋๋ผ error๋ฅผ throwํจ
-
๊ธฐ์ฌ๋์ด์์ง / ์กด์ฌํ์ง ์๋ ๋ณ์์ ๋ํ ์ ๊ทผ ๋ถ๊ฐ
-
๋ณ์, ํจ์ ์ญ์ ๋ถ๊ฐ
-
๋๊ฐ์ ๋งค๊ฐ๋ณ์ ๋ถ๊ฐ
-
์ซ์ ์์ 0๋ถ๊ฐ
-
readonly ๋ณ์๋ ์ฐ๊ธฐ ์ฐ์ฐ ๋ถ๊ฐ
var obj = {}; Object.defineProperty(obj, "x", {value:0, writable:false}); obj.x = 3.14; // ๋ถ๊ฐ
-
get์ผ๋ก๋ง ์ป์ ์ ์๋ ๋ณ์๋ ๋ฐ๋ก ์ธ ์ ์์
var obj = {get x() {return 0} }; obj.x = 3.14; // ๋ถ๊ฐ
-
๋ณ์ ์ด๋ฆ์ผ๋ก ๋ถ๊ฐํ ๊ฒ๋ค
- implements
- interface
- let
- package
- private
- protected
- public
- static
- yield
-
this๋ ํด๋น function์ ํธ์ถํ object๋ฅผ ์๋ฏธํ๊ธฐ ๋๋ฌธ์, ํด๋น object๊ฐ ๋ช ํํ์ง ์์ผ๋ฉด this๊ฐ undefined๊ฐ ๋จ
2.2.2. String.trim()
let str = " hello heejin ";
alert(str.trim());
์์ชฝ์ ๋น์นธ์ ์์ ์ค
IE8์์๋ ์ ๊ทํํ์์ผ๋ก ๋์ฒดํด์ผํจ
2.2.3. Array.isArray(object)
object๊ฐ array์ธ์ง ์ฒดํฌ
2.2.4. Array.forEach(func)
forEach์ ๋๊ฒจ์ง ๋งค๊ฐ๋ณ์ function์ด Array์ ๊ฐ ์์๋ง๋ค ์คํ๋จ
function์๋ ๋งค๊ฐ๋ณ์ value๊ฐ ์์
2.2.5. Array.map(func)
map์ ๋๊ฒจ์ง ๋งค๊ฐ๋ณ์ function์ด Array์ ๊ฐ ์์๋ง๋ค ์คํ๋๊ณ , ์คํ ๊ฒฐ๊ณผ๋ฅผ ์๋ก์ด Array๋ก returnํจ
function์๋ ๋งค๊ฐ๋ณ์ value, index, array๊ฐ ์์
2.2.6. Array.filter(func)
filter์ ๋๊ฒจ์ง ๋งค๊ฐ๋ณ์ test function์ ํต๊ณผํ ์์๋ง ๋ค์ ์ ์ฅํ์ฌ, ์คํ ๊ฒฐ๊ณผ๋ฅผ ์๋ก์ด Array๋ก return ํจ
function์๋ ๋งค๊ฐ๋ณ์ value, index, array๊ฐ ์์
2.2.7. Array.reduce(func)
ํ ๊ฐ์ง ๋ณ์๋ฅผ ์์ฑํ๊ธฐ ์ํด Array์ ์์๋ฅผ ๋ฐ๋ณตํด์ ๊ฐ์ ธ์ค๋ ๊ฒฝ์ฐ.
reduce์ ๋๊ฒจ์ง ๋งค๊ฐ๋ณ์ function์ ํตํด ์๋ก ์ ์ํ ๋ณ์๋ฅผ ๋๊ฒจ๋ฐ๊ณ ๊ฒฐ๊ณผ๋ก return ํจ
var numbers1 = [45, 4, 9, 16, 25];
var sum = numbers1.reduce(myFunction);
function myFunction(total, value, index, array) {
return total + value;
}
function์๋ ๋งค๊ฐ๋ณ์ total, value, index, array๊ฐ ์์
total์ ์ด๊ธฐ ๋ณ์์ด์ ์ด์ ์์์ function์์ return๋ ๊ฐ์
2.2.8. Array.reduceRight(func)
์ค๋ฅธ์ชฝ์์ ์ผ์ชฝ์ผ๋ก ๋์
2.2.9. Array.every(func)
๋ชจ๋ ์์๊ฐ ๋งค๊ฐ๋ณ์๋ก ๋๊ฒจ์ง test function์ ๋ง์กฑํ๋์ง boolean์ผ๋ก return
2.2.10. Array.some(func)
๋งค๊ฐ๋ณ์๋ก ๋๊ฒจ์ง test function์ ๋ง์กฑํ๋ ์์๊ฐ ์๋์ง boolean์ผ๋ก return
2.2.11. Array.indexOf(item, [start])
์ฐพ๊ณ ์ํ๋ ์์๋ฅผ ๋งค๊ฐ๋ณ์๋ก ๋๊ธฐ๋ฉด ํด๋น ์์๋ฅผ start๋ถํฐ ์ฐพ์์ ์ฒซ index๋ฅผ return
2.2.12. Array.lastIndexOf(item, [start])
์ฐพ๊ณ ์ํ๋ ์์๋ฅผ ๋งค๊ฐ๋ณ์๋ก ๋๊ธฐ๋ฉด ํด๋น ์์๋ฅผ start๋ถํฐ ์ฐพ์์ ๋ง์ง๋ง index๋ฅผ return
2.2.13. JSON.parse(string)
string์ json object๋ก ๋ณํ
2.2.14. JSON.stringify(object)
object๋ฅผ string์ผ๋ก
2.2.15. Date.now()
milliseconds ๋จ์์ date return
2.2.16. Property Getters and Setters
// Create an object:
var person = {
firstName: "John",
lastName : "Doe",
get fullName() {
return this.firstName + " " + this.lastName;
}
};
// Display data from the object using a getter:
document.getElementById("demo").innerHTML = person.fullName;
2.2.17. New Object Property Methods
Object.defineProperty(๋ณ์, ์์ฑ, ๋ฐ๊ฟ ์์ฑ)
๋ก object์ ์์ฑ์ ๋ณ๊ฒฝํ ์ ์์ผ๋ฉฐ, ์ฝ๊ธฐ ์ฐ๊ธฐ ๋ณ๊ฒฝ ๊ถํ๋ ์กฐ์ ํ ์ ์์
// Change a Property:
Object.defineProperty(person, "language", {
value: "EN",
writable : true,
enumerable : true,
configurable : true
});
์ถ๊ฐ๋ ๋ฉ์๋๋ค
// Adding or changing an object property
Object.defineProperty(object, property, descriptor)
// Adding or changing many object properties
Object.defineProperties(object, descriptors)
// Accessing Properties
Object.getOwnPropertyDescriptor(object, property)
// Returns all properties as an array
Object.getOwnPropertyNames(object)
// Returns enumerable properties as an array
Object.keys(object)
// Accessing the prototype
Object.getPrototypeOf(object)
// Prevents adding properties to an object
Object.preventExtensions(object)
// Returns true if properties can be added to an object
Object.isExtensible(object)
// Prevents changes of object properties (not values)
Object.seal(object)
// Returns true if object is sealed
Object.isSealed(object)
// Prevents any changes to an object
Object.freeze(object)
// Returns true if object is frozen
Object.isFrozen(object)
2.2.18. string ์์ฑ ์ ๊ทผ
- string.charAt(index) : index์ ์๋ character ๋ฐํ
- charAt ์ฐ์ฐ์๋ ์์ผ๋ฉด empty string return
- string.charCodeAt(index) : ํด๋น index์ ๊ธ์์ UTF-16 code ๋ฐํ
- string[] ์ผ๋ก ์ ๊ทผ ๊ฐ๋ฅ
- IE7 ์ ์๋ ๋์ x
- [] ์ฐ์ฐ์๋ ์์ผ๋ฉด undefined return
- readonly => ํด๋น ์ฐ์ฐ์๋ก ๊ฐ ํ ๋น x
3. ES6
3.0.1. let, const
let, const๋ block ๋ฒ์ ๋ด์์ ์ ์ํ ์ ์์
์ฌ์ ์ ๋ถ๊ฐ
let์ ๊ฐ ์ฌํ ๋น ๊ฐ๋ฅ, const๋ ๋ถ๊ฐ
3.0.2. arrow function
const a = (x, y) => x*y;
arrow function์ this ๊ฐ ๊ธฐ๋ณธ function๊ณผ ๋ค๋ฆ
๊ธฐ๋ณธ function์ ํด๋น function์ callํ object
arrow function์ ํด๋น function์ owner
๋ณธ๋ js๋ hoisting(๋์ด์ฌ๋ ค ์ฌ์ฉํ๊ธฐ)์ด ๊ฐ๋ฅํ๋ฐ, arrow function์ ๋ถ๊ฐ
return ํค์๋์ {}๋ฅผ ์๋ต ๊ฐ๋ฅ
3.0.3. Loop Array, String
for (x of cars) {
...
}
for (x of txt) {
...
}
- for in : object์ ์ ์ฉ๋์ด index๋ฅผ ๋ฐ์์ด
- for of : iterable object์ ์ ์ฉ๋์ด ์์๋ฅผ ๋ฐ์์ด
- string, array, map, nodelist ๋ฑ๋ฑ ๊ฐ๋ฅ
3.0.4. JS classes
class ํค์๋๋ฅผ ์ฌ์ฉํด์ class๋ฅผ ๋ง๋ค ์ ์๊ฒ ๋จ
constructor function์ ํญ์ ํฌํจํ๊ณ ์์ด์ผ ํจ
object๊ฐ ์๋๋ผ object์ template์ผ๋ก ์์ฉ
์ฌ์ฉ ๋ฐฉ์์ ์ฌ๋ ์ธ์ด๋ค๊ณผ ๊ฐ์
3.0.5. JS Promises
let myPromise = new Promise(function(myResolve, myReject) {
myResolve(); // when successful
myReject(); // when error
});
myPromise.then(
function(value) { /* code if successful */ },
function(error) { /* code if some error */ }
);
3.0.6. Symbol type
object์ ์จ์ identifier๋ฅผ ๋ง๋ค ๋ ์ฌ์ฉ
const person = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};
let id = Symbol('id');
person.id = 140353;
ํญ์ uniqueํ ๊ฐ์ ๊ฐ์ ธ์ผ ํจ
3.0.7. default parameter values
๋งค๊ฐ๋ณ์ ๋๊ธธ ๋ default ๊ฐ ์ง์ ๊ฐ๋ฅ
3.0.8. rest parameter
function sum(...args){
}
ํด๋น ํจ์๋ ๋งค๊ฐ๋ณ์์ ๊ฐฏ์๊ฐ ์ ํด์ง์ง ์์
3.0.9. Array.find(func)
๋งค๊ฐ๋ณ์๋ก ๋๊ฒจ์ง function์ ํต๊ณผํ ์ฒซ๋ฒ์งธ ์์ ๋ฐํ
function์ ๋งค๊ฐ๋ณ์๋ value, index, array
3.0.10. Array.findIndex(func)
์์ ๊ฐ๊ณ , index๋ฅผ ๋ฐํ
3.0.11. New Number Properties
- EPSILON
- MIN_SAFE_INTEGER
- MAX_SAFE_INTEGER
3.0.12. New Number Method
- Number.isInteger(number) : ์ ์์ธ์ง ํ์ธ
- Number.isSafeInteger(number) : ์์ ํ ๋ฒ์ ๋ด์ ์ ์์ธ์ง ํ์ธ
3.0.13. New Global Methods
- isFinite() : Infinity ๋๋ NaN์ด ์๋์ง ํ์ธ
- isNaN() : NaN์ด ์๋์ง ํ์ธ
4. ES7
4.0.1. string.padStart(size, paddingChar)
paddingChar๋ฅผ size-1๋งํผ ์์ padding
4.0.2. string.padEnd(size, paddingChar)
paddingChar๋ฅผ size-1๋งํผ ๋ค์ padding
Comment