What is “assert” in JavaScript?

后端 未结 14 728
-上瘾入骨i
-上瘾入骨i 2020-12-04 05:03

What does assert mean in JavaScript?

I’ve seen something like:

assert(function1() && function2() && function3(), \"some          


        
相关标签:
14条回答
  • 2020-12-04 05:22

    If you use webpack, you can just use the node.js assertion library. Although they claim that it's "not intended to be a general purpose assertion library", it seems to be more than OK for ad hoc assertions, and it seems no competitor exists in the Node space anyway (Chai is designed for unit testing).

    const assert = require('assert');
    ...
    assert(jqXHR.status == 201, "create response should be 201");
    

    You need to use webpack or browserify to be able to use this, so obviously this is only useful if those are already in your workflow.

    0 讨论(0)
  • 2020-12-04 05:25

    If the assertion is false, the message is displayed. Specifically, if the first argument is false, the second argument (the string message) will be be logged in the developer tools console. If the first argument is true, basically nothing happens. A simple example – I’m using Google Developer Tools:

    var isTrue = true;
    var isFalse = false;
    console.assert(isTrue, 'Equals true so will NOT log to the console.');
    console.assert(isFalse, 'Equals false so WILL log to the console.');
    
    0 讨论(0)
  • 2020-12-04 05:27

    If using a modern browser or nodejs, you can use console.assert(expression, object).

    For more information:

    • Chrome API Reference
    • Firefox Web Console
    • Firebug Console API
    • IE Console API
    • Opera Dragonfly
    • Nodejs Console API
    0 讨论(0)
  • 2020-12-04 05:27

    The other answers are good: there isn't an assert function built into ECMAScript5 (e.g. JavaScript that works basically everywhere) but some browsers give it to you or have add-ons that provide that functionality. While it's probably best to use a well-established / popular / maintained library for this, for academic purposes a "poor man's assert" function might look something like this:

    const assert = function(condition, message) {
        if (!condition)
            throw Error('Assert failed: ' + (message || ''));
    };
    
    assert(1 === 1); // Executes without problem
    assert(false, 'Expected true');
    // Yields 'Error: Assert failed: Expected true' in console
    
    0 讨论(0)
  • 2020-12-04 05:29

    There is no assert in JavaScript (yet; there's talk of adding one, but it's at an early stage). Perhaps you're using some library that provides one. The usual meaning is to throw an error if the expression passed into the function is false; this is part of the general concept of assertion checking. Usually assertions (as they're called) are used only in "testing" or "debug" builds and stripped out of production code.

    Suppose you had a function that was supposed to always accept a string. You'd want to know if someone called that function with something that wasn't a string. So you might do:

    assert(typeof argumentName === "string");
    

    ...where assert would throw an error if the condition were false.

    A very simple version would look like this:

    function assert(condition, message) {
        if (!condition) {
            throw message || "Assertion failed";
        }
    }
    

    Better yet, make use of the Error object if the JavaScript engine supports it (really old ones might not), which has the advantage of collecting a stack trace and such:

    function assert(condition, message) {
        if (!condition) {
            message = message || "Assertion failed";
            if (typeof Error !== "undefined") {
                throw new Error(message);
            }
            throw message; // Fallback
        }
    }
    

    Even IE8 has Error (although it doesn't have the stack property, but modern engines [including modern IE] do).

    0 讨论(0)
  • 2020-12-04 05:29

    Here is a really simple implementation of an assert function. It takes a value and a description of what you are testing.

     function assert(value, description) {
            var result = value ? "pass" : "fail";
            console.log(result + ' - ' +  description); 
        };
    

    If the value evaluates to true it passes.

    assert (1===1, 'testing if 1=1');  
    

    If it returns false it fails.

    assert (1===2, 'testing if 1=1');
    
    0 讨论(0)
提交回复
热议问题