자바스크립트의 배열 [ ] 의 경우에는


length 를 통하여 길이를 간단하고 손쉽게 가져올수 있다.


하지만 obj의 경우에는 length 를 지정하게 되면 undefined 가 나타납니다.


obj 의 경우 length 의 길이를 구하기 위해서는


Object.keys를 이용해야 합니다.


 

var objS = {name:"test",ttt:"NewTest",kim:"kim"};

console.log("obj.length : ",objS.length);
console.log("Object.keys Length : ",Object.keys(objS).length);

Object.keys에 대해 간단하게 적어 보면


Object.keys(obj)

obj -> 열거형 고유 속성이 반환될 개체


return Value -> 지정된 개체의 모든 나열형 속성들을 나타내는 문자열 배열

나열형 속성들을 나타낸다고 되어있다.


속성열 나열 테스트


var myObj = Object.create({}, {
  getFoo: {
    value: function () { return this.foo; }
  } 
});
myObj.foo  = 1;
myObj.foo2 = 2;
myObj.foo3 = 3;

console.log(myObj);
console.log(Object.keys(myObj)); 


결과

{foo: 1, foo2: 2, foo3: 3, getFoo: ƒ}

(3) ["foo", "foo2", "foo3"]



Posted by 깍돌이

path 모듈입니다.

The path module provides utilities for working with file and directory paths. It can be accessed using:

const path = require('path');

기본 모듈 생성 불러 오는 방법입니다.


우선적으로 


path.basename 에 대해서 알아 볼 예정인데요


path.basename은 파일명을 가져오는 메서드입니다. 해당 메서드는 Windows 와 POSIX에서의 사용법이 다르다고합니다.

On POSIX:

path.basename('C:\\temp\\myfile.html');
// Returns: 'C:\\temp\\myfile.html'

On Windows:

path.basename('C:\\temp\\myfile.html');
// Returns: 'myfile.html'


윈도우즈에서는 기본적으로 파일 네임을 가져오는 것으로 보입니다.(확장자 포함)


하지만 POSIX에서는 파일네임이 아니라 전체 경로를 가져오게 됩니다.


해당 차이점을 알고 쓰셔야 할것 같지만 Node.js의 공식 문서를 참고 하시면


모든 OS에서 Windows 와 같은 결과를 받아오게 하기 위한 가이드가 있습니다.


On POSIX and Windows:

path.win32.basename('C:\\temp\\myfile.html');
// Returns: 'myfile.html'

To achieve consistent results when working with POSIX file paths on any operating system, use path.posix:

On POSIX and Windows:

path.posix.basename('/tmp/myfile.html');
// Returns: 'myfile.html'


앞에 path.win32.basename 또는 path.posix.basename  해당 os에 대해 나타나게 합니다.


저는 WIndows 에서 테스트를 하였습니다.



path.basename 보다는 posix나 win32를 넣는것이 사용에 좋아보입니다.


path 모듈에서는 여러가지 사용 메서드들이 있지만 관련은 www.nodejs.org 에서 공식문서를 확인해보시기 바랍니다.


하나 더 괜찮아 보이는 메서드를 가져왔습니다.

path.parse(path)


parse입니다. (path 값을 파라미터로 받습니다.)


POSIX입니다.

For example on POSIX:

path.parse('/home/user/dir/file.txt');
// Returns:
// { root: '/',
//   dir: '/home/user/dir',
//   base: 'file.txt',
//   ext: '.txt',
//   name: 'file' }
┌─────────────────────┬────────────┐
│          dir        │    base    │
├──────┬              ├──────┬─────┤
│ root │              │ name │ ext │
"  /    home/user/dir / file  .txt "
└──────┴──────────────┴──────┴─────┘
(all spaces in the "" line should be ignored -- they are purely for formatting)

WIndows 입니다.

On Windows:

path.parse('C:\\path\\dir\\file.txt');
// Returns:
// { root: 'C:\\',
//   dir: 'C:\\path\\dir',
//   base: 'file.txt',
//   ext: '.txt',
//   name: 'file' }
┌─────────────────────┬────────────┐
│          dir        │    base    │
├──────┬              ├──────┬─────┤
│ root │              │ name │ ext │
" C:\      path\dir   \ file  .txt "
└──────┴──────────────┴──────┴─────┘
(all spaces in the "" line should be ignored -- they are purely for formatting)


path 모듈을 알아 봤습니다.


'Node.js > NPM 사용 ' 카테고리의 다른 글

4. nconf (시스템 환경 변수 모듈)  (0) 2018.03.14
3. body-parser  (0) 2017.11.30
2. cors (Cross Origin Resource Sharing)  (0) 2017.11.29
1. uglify-js (경량화)  (0) 2017.11.08
Posted by 깍돌이

https://docs.npmjs.com/files/package.json


공식 홈페이지

Posted by 깍돌이

블로그 이미지
QA를 업으로 삼고 있습니다.
깍돌이

태그목록

Yesterday
Today
Total

달력

 « |  » 2024.5
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함