JavaScript is a line item

When looking at adding a cropping function to a String prototype, I came across something that seems strange to me with JavaScript strings.

if (typeof console === 'undefined') {
    var console = { };
    console.log = function(msg) {
        alert(msg)
    }
}


function isString(str) {
    return ((str && typeof str === 'string') || 
        (str && (str.constructor == String && (str.toString() !== 'null' && str.toString() !== 'undefined'))));
}

if (!String.prototype.trim) {
    String.prototype.trim = function () {
        return this.replace(/^\s*(\S*(?:\s+\S+)*)\s*$/, "$1");
    };
}
function testing (str) {
    if (isString(str)) {
        console.log("Trimmed: " + str.trim() + " Length: " + str.trim().length);
    } else {
        console.log("Type of: " + typeof str);
    }
    return false;
}

function testSuite() {
    testing(undefined);
    testing(null);
    testing("\t\r\n");
    testing("   90909090");
    testing("lkkljlkjlkj     ");
    testing("    12345       ");
    testing("lkjfsdaljkdfsalkjdfs");
    testing(new String(undefined));                //Why does this create a string with value 'undefined'
    testing(new String(null));                     //Why does this create a string with value 'null'
    testing(new String("\t\r\n"));
    testing(new String("   90909090"));
    testing(new String("lkkljlkjlkj     "));
    testing(new String("    12345       "));
    testing(new String("lkjfsdaljkdfsalkjdfs"));
}

Now I know that we should not create lines with a new operator, but I would be embarrassed if someone called it undefined or an empty line that was created more line by line:

    new String ( someUndefinedOrNullVar );

What am I missing? Or that! == 'null' && &! == 'undefined' check is really necessary (Removing this check will show 'null' and 'undefined')?

+3
source share
3 answers

From ECMA standard :

9.8 ToString
The abstract operation ToString converts its argument to a value of type String according to Table 13 
[ the table shows undefined converts to "undefined" and null to "null"]

... and then:

15.5.2.1 new String ( [ value ] )
The [[Prototype]] internal property of the newly constructed object is set to the standard built-in String prototype object that is the initial value of String.prototype (15.5.3.1).
The [[Class]] internal property of the newly constructed object is set to "String".
The [[Extensible]] internal property of the newly constructed object is set to true.
The [[PrimitiveValue]] internal property of the newly constructed object is set to ToString(value), or to the empty String if value is not supplied.

As it ToString(undefined)gives 'undefined', it makes sense.

+4

, String (null) String (undefined) , : 'null' 'undefined'.

: , null - . , undefined.

0

JavaScript , new String(null). !== 'null' && !== 'undefined' , ... .

'' + null // 'null'
'' + undefined // 'undefined'
[null].join() // 'null'

. foolproving trim(), , , - 'null' 'undefined', , , . , !

0

Source: https://habr.com/ru/post/1762934/


All Articles