简体   繁体   中英

How do I tell if my Javascript code is running in Jint?

I've designed some functions which call alert() and confirm().

I use Jint to test those functions.

However, Jint doesn't support alert() and confirm().

What I'd like to do is create a wrapper around alert() and confirm() which checks if running in Jint context:

function MyAlert()
{
    if(Jint != true) alert();
}

How do I tell if I'm in Jint context?

Thanks, Ed

Rather than environment detection, I'd suggest feature detection:

function MyAlert(msg) {
    if (typeof alert !== "undefined") { // *
        alert(msg);
    }
}

* or === "function" , but some older browsers (like IE8, still sadly in significant-though-diminishing use) report built-ins like alert as "object" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM