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.

48 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://v8.dev/docs/stack-trace-api)
  3. ## Install
  4. ```
  5. $ npm install 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. - `getThis`: returns the value of `this`.
  19. - `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.
  20. - `getFunction`: returns the current function.
  21. - `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.
  22. - `getMethodName`: returns the name of the property of `this` or one of its prototypes that holds the current function.
  23. - `getFileName`: if this function was defined in a script returns the name of the script.
  24. - `getLineNumber`: if this function was defined in a script returns the current line number.
  25. - `getColumnNumber`: if this function was defined in a script returns the current column number
  26. - `getEvalOrigin`: if this function was created using a call to `eval` returns a string representing the location where `eval` was called.
  27. - `isToplevel`: is this a top-level invocation, that is, is this the global object?
  28. - `isEval`: does this call take place in code defined by a call to `eval`?
  29. - `isNative`: is this call in native V8 code?
  30. - `isConstructor`: is this a constructor call?
  31. ## License
  32. MIT © [Sindre Sorhus](https://sindresorhus.com)