You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.8 KiB

4 years ago
  1. # callsites [![Build Status](https://travis-ci.org/sindresorhus/callsites.svg?branch=master)](https://travis-ci.org/sindresorhus/callsites)
  2. > Get callsites from the [V8 stack trace API](https://github.com/v8/v8/wiki/Stack-Trace-API)
  3. ## Install
  4. ```
  5. $ npm install --save callsites
  6. ```
  7. ## Usage
  8. ```js
  9. const callsites = require('callsites');
  10. function unicorn() {
  11. console.log(callsites()[0].getFileName());
  12. //=> '/Users/sindresorhus/dev/callsites/test.js'
  13. }
  14. unicorn();
  15. ```
  16. ## API
  17. Returns an array of callsite objects with the following methods:
  18. - `getTypeName`: returns the type of this as a string. This is the name of the function stored in the constructor field of this, if available, otherwise the object's [[Class]] internal property.
  19. - `getFunctionName`: returns the name of the current function, typically its name property. If a name property is not available an attempt will be made to try to infer a name from the function's context.
  20. - `getMethodName`: returns the name of the property of this or one of its prototypes that holds the current function
  21. - `getFileName`: if this function was defined in a script returns the name of the script
  22. - `getLineNumber`: if this function was defined in a script returns the current line number
  23. - `getColumnNumber`: if this function was defined in a script returns the current column number
  24. - `getEvalOrigin`: if this function was created using a call to eval returns a CallSite object representing the location where eval was called
  25. - `isToplevel`: is this a top-level invocation, that is, is this the global object?
  26. - `isEval`: does this call take place in code defined by a call to eval?
  27. - `isNative`: is this call in native V8 code?
  28. - `isConstructor`: is this a constructor call?
  29. ## License
  30. MIT © [Sindre Sorhus](https://sindresorhus.com)