How to elegantly throw errors in JS? [closed]

3 days ago 6
ARTICLE AD BOX

Just a example.

If I have a function to do some services,and this function should check param type like this:

function did(p) { if(type of p is not ok) throw Error(); }

It's good to record stack trace in error.

If I want to reuse this type check,I maybe write a assert function and use it like this:

function assert(p) { if(type of p is not ok) throw Error(); } function did(P) { assert(p); }

But it will make stack trace in 'assert' function not in 'did' function.If I make more complex assert function,the stack trace will have more noise.

Is there good practice for it?

Read Entire Article