붉은거위 노트 (redgoose note)

자바스크립트 노트 (javascript note)

Nest
Development
Category
Javascript
Hit
663
Star
0

특정 영역의 수 사이를 난수로 만들어내는 함수

function randomRange(n1, n2) {
  return Math.floor( (Math.random() * (n2 - n1 + 1)) + n1 );
}
randomRange(0,3);

IE에서 console.log메서드를 사용할때 오류 해결하기

if (window.console == undefined) {
  console = { log : function(){} };
}

배열속에 있는 숫자중에 가장 큰수를 찾기

Array.prototype.max = function () {
  return Math.max.apply(Math, this);
};
console.log(array.max());

Object를 getdata형태로 변환

function objectToPost(obj) {
  var result = '';
  for (var i in obj) {
    result += i + '=' + obj[i] + '&';
  }
  return result;
}

10 이하의 숫자는 0x로 변환

function getFormattedPartTime(n) {
  if (10 > n) {
    return '0' + n;
  }
  return n;
}

20131215 -> 2013-12-15로 변환

function stringToDate(str) {
  return str.substr(0, 4) + '-' + str.substr(4, 2) + '-' + str.substr(6, 2);
}

디바이스가 ios인지 체크하기

let iOS = (navigator.userAgent.match(/(iPad|iPhone|iPod)/i) ? true : false);

브라우저가 무엇인지 검사하여 브라우저 이름을 html.class로 표시해준다.

https://code.google.com/p/css-browser-selector

Array객체에서 놓치기 쉬운 6개의 메서드

http://programmingsummaries.tistory.com/357

기본적인 스크립트만으로 드롭다운 메뉴를 구현할 수 있다.

https://www.w3schools.com/howto/howto_js_dropdown.asp

debounce 함수에 관한 아티클

https://chaewonkong.github.io/posts/debounce-js.html

자식 요소를 선택하기

스크립트로 변수화된 엘리먼트의 자식을 선택하는 방법

el.querySelector(':scope > div');

타임존을 작용한 시간 설정하기

노드에서 그냥 시간을 new Date()로 설정하면 뭔가 시간이 어긋나있다. 타임존 때문에 그런듯한데 다음과 같이 밀리세컨드에서 타임존 시간을 빼서 설정하면 잘 되는듯하다.

let date = new Date(1446309338000 - new Date(1446309338000).getTimezoneOffset() * 60 * 1000)

출처: https://stackoverflow.com/a/33507809

배열속 요소를 랜덤하게 한개 뽑아오기

/**
 * random pick in array
 * @param {array} arr
 * @return {any}
 */
function randomPickInArray(arr = [])
{
  return arr[Math.floor(Math.random() * arr.length)]
}

const pick = randomPickInArray([ 0, 1, 2, 3, 4, 5 ])

import 모듈 이름을 바꿔쓰기

import로 모듈을 가져올때 이름을 바꿔쓰고 싶어질때가 있다. 그럴때 다음과 같이 작성하면 된다.

import { setupEditorCore, instance as editorCore } from './module.mjs'