Skip to content

Commit

Permalink
Debug in JavaScrip
Browse files Browse the repository at this point in the history
  • Loading branch information
steelywing committed Aug 1, 2024
1 parent be43ac1 commit 26f3f0f
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions blog/Debug in JavaScrip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
---
date: 2024-08-01
tags: [JavaScript, Debug]
---

# Debug in JavaScrip

<!--truncate-->

## `debugger`

In browser, `debugger` can trigger breakpoint

```js
debugger;
```

## Debug native function

```js
function debug(fn) {
return function () {
debugger;
return fn.apply(this, arguments);
};
}
```

To debug `"string".at()`

```js
String.prototype.at = debug(String.prototype.at);
```

To debug `console.log()`

```js
console.log = debug(console.log);
```

## Inject function

```js
function inject(to, fn) {
return function () {
fn.apply(this, arguments);
return to.apply(this, arguments);
};
}
```

To log the call stack and arguments when `"string".replace()` is called

```js
String.prototype.replace = inject(
String.prototype.replace,
function () {
console.trace();
console.log(arguments);
}
);

"abc".replace("a", "s");
```

## Call stack trace

Get the call stack

```js
new Error().stack
```

Print the call stack

```js
console.trace();

console.trace("message");
```

## Debug in node.js

```cli
node --inspect server.js
```

0 comments on commit 26f3f0f

Please sign in to comment.