boostcourse 생활코딩: 자바스크립트의 시작


  • Built-in 객체
  • 개발자의 편의를 위해 JavaScript 자체적으로 지원하는 객체

수학적인 작업에 필요한 변수나 함수를 제공

console.log(Math.PI);
// 3.141592653589793
 
console.log(Math.random());
console.log(Math.random());
console.log(Math.random());
// 0.6374999037582858
// 0.0979461270726425
// 0.6265934828152089
 
console.log(Math.floor(3.9));
// 3

  • 이외에도 수학, 문자, 날짜, 숫자, error 등 다양한 내장 객체가 존재한다.
  • MDN 사이트에서 다양한 내장 객체들을 확인할 수 있다.

MyMath라는 객체를 만들고, 그 안에 변수와 메서드를 만들어보자.

// MyMath
var MyMath = {
  PI: Math.PI,
  random: function () {
    return Math.random();
  },
  floor: function (val) {
    return Math.floor(val);
  },
};
 
console.log(MyMath.PI);
console.log(MyMath.random());
console.log(MyMath.floor(4.9));

결과

3.141592653589793
0.10007347046764292
4

Math와 비슷하게 하기 위해 안에서 다시 Math 객체의 각 변수와 메서드를 가져와 return하였다. 만약 실제 코드를 짠다면 내가 원하는 코드를 짜넣을 수 있을 것이다.