Reasons loopback0 shouldn't listen to SO about JavaScript (or anything probably)



  • Continuing the discussion from Del/Ins corrector - Composer Extension (UserScript):

    @accalia said:

    @loopback0 said:
    String.fromCharCode(13)

    don't you mean '\n' there? (it's shorter and more recognizeable)



  • I Googled how to do a newline with JavaScript and SO said \n gets entered as the string \n and to use that instead.

    I don't do JavaScript so I took it at face value.


  • sockdevs

    @loopback0 said:

    I Googled how to do a newline with JavaScript and SO said \n gets entered as the string \n and to use that.

    :wtf:
    There's 'wrong', there's 'very wrong', and there's 'keep 100 yards away from all keyboards'; this is the third one


  • sockdevs

    @loopback0 said:

    SO said \n gets entered as the string \n and to use that.

    :wtf: no...... that SO is wrong. (they so often are for JS it's not even funny. ;-))

    '\n' => newline
    '\\n' => '\n' (literally, not escape code here)



  • And that's probably obvious to someone who actually uses the language. Someone who quickly wants to hack together a button to insert some shit in a language they don't use isn't going to know that, so I used it.
    It works.


  • sockdevs

    True, but presumably the person posting the answer knows- actually, y'know what, it's SO, that's not at all guaranteed…



  • The solution isn't wrong even if the reasoning behind it is. And all I was after was the solution.


  • sockdevs

    the solution worked, yes.

    but the reasoning why the simpler solution wouldn't work was way wrong.



  • This post is deleted!


  • @Zecc said:

    (post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

    Ye... I mean... (post withdrawn by author, will be automatically deleted in 24 hours unless flagged)



  • :eek:

    That's the sort of thing I would have tried both ways just to see what's what. But I would actually assumed that \n would work and have been astonished when it broke (assuming the counter-factual here, of course).



  • I almost just tried \n out of habit but thought I'd check. The solution worked, I didn't look into it after that.


  • :belt_onion:

    @loopback0 said:

    I Googled how to do a newline with JavaScript and SO said \n gets entered as the string \n and to use that instead.

    Is there an actual edge case where this might happen? I don't know of one, but it's JavaScript, I wouldn't be horribly surprised.



  • @Onyx said:

    Is there an actual edge case where this might happen?

    You're asking the wrong person :laughing:


  • sockdevs

    In C#, there's verbatim strings, where escape sequences aren't parsed; I don't think JavaScript has a feature like that though


  • :belt_onion:

    I'm asking the general public, but quoting the relevant bit for context.

    You are not the centre of the Universe*, you uncivilized clod!


    * We all are, technically, since the Universe expands uniformly when observed from any point.



  • @Onyx said:

    I'm asking the general public, but quoting the relevant bit for context.

    I figured but that is :barrier: to replying.

    This was the result I found.

    Edge case? Idiot? Who knows.


  • :belt_onion:

    @loopback0 said:

    I figured but that is :barrier: to replying.

    /t/1000 is close. Just follow the cooties.

    Also, NOREPRO in Opera (WebKit), though I didn't try that hard.

    Edit: Actual screenies:

    Unless it has to be a new node? CBA to check.





  • Also, is the \n thing broken because you used ' instead of "? I forget if that makes a difference in JS liike it does in PHP.



  • TRWTF is <br /> instead of <br/>.


  • sockdevs

    @boomzilla said:

    TRWTF is <br /> instead of <br/>.

    IIRC, some older browsers would baulk on <br/>, but be fine with <br />; it probably doesn't apply anymore though


  • :belt_onion:

    @powerlord said:

    Also, is the \n thing broken because you used ' instead of "? I forget if that makes a difference in JS liike it does in PHP.

    I used ' on purpose in my testing. Still worked.

    AFAIK, there is no difference anyway in JS.


  • mod

    @Onyx said:

    there is no difference anyway in JS.

    Correct


  • :belt_onion:

    For completeness' sake, so the people who don't have to deal with PHP understand the potential confusion:

    php > $someVar = 5;
    php > echo "Value of $someVar";
    Value of 5
    php > echo 'Value of $someVar';
    Value of $someVar
    


  • From memory, here's a better example:

    php > echo "\n";
    
    
    php > echo '\n';
    \n

  • sockdevs

    @Onyx said:

    AFAIK, there is no difference anyway in JS.

    @Yamikuronue said:

    Correct

    well.... on a US qwerty layout you have to press SHIFT to get " where ' doesn't. also you can embed unescaped " in a ' string and you cna embed an unescaped ' in a " string.

    other than that there's no difference. now the new template strings in ES6 are a whole new beast and i want them in nodeJS because :heart_eyes_cat:


  • sockdevs

    @accalia said:

    other than that there's no difference. now the new template strings in ES6 are a whole new beast and i want them in nodeJS because :heart_eyes_cat:

    Because what JS really needs is a feature from PHP :laughing:


  • sockdevs

    @RaceProUK said:

    Because what JS really needs is a feature from PHP

    it is an awesome feature though.

    how many times have i written

    return '\nIn the last ' + time_period + '\n' +
        'Visits:              ' + days_visited + '/' + min_days_visited + '\n' +
        'Topics Replied To:   ' + num_topics_replied_to + '/' + min_topics_replied_to + '\n' +
        'Topics Viewed:       ' + topics_viewed '+/' + min_topics_viewed + '\n' +
        'Total Topics Viewed: ' + topics_viewed_all_time + '/' + min_topics_viewed_all_time + '\n' +
        'Posts Read:          ' + posts_read + '/' + min_posts_read + '\n' +
        'Total Posts Read:    ' + posts_read_all_time + '/' + min_posts_read_all_time + '\n' +
        'Flagged Posts:       ' + num_flagged_posts + '/' + max_flagged_posts + ' (max)\n' +
        'Flagged By:          ' + num_flagged_by_users + '/' + max_flagged_by_users + ' (max)\n' +
        'Likes Given:         ' + num_likes_given '+/' + min_likes_given + '\n' +
        'Likes Received:      ' + num_likes_received '+/' + min_likes_received + '\n' +
        'Liked on Days:       ' + num_likes_received_days + '/' + min_likes_received_days + '\n' +
        'Liked by users:      ' + num_likes_received_users + '/' + min_likes_received_users + '\n';
    

    when i could write it this way with template strings:

    return `\nIn the last ${time_period}
        Visits:              ${days_visited}/${min_days_visited}
        Topics Replied To:   ${num_topics_replied_to}/${min_topics_replied_to}
        Topics Viewed:       ${topics_viewed}/${min_topics_viewed}
        Total Topics Viewed: ${topics_viewed_all_time}/${min_topics_viewed_all_time}
        Posts Read:          ${posts_read}/${min_posts_read}
        Total Posts Read:    ${posts_read_all_time}/${min_posts_read_all_time}
        Flagged Posts:       ${num_flagged_posts}/${max_flagged_posts} (max)
        Flagged By:          ${num_flagged_by_users}/${max_flagged_by_users} (max)
        Likes Given:         ${num_likes_given}/${min_likes_given}
        Likes Received:      ${num_likes_received}/${min_likes_received}
        Liked on Days:       ${num_likes_received_days}/${min_likes_received_days}
        Liked by users:      ${num_likes_received_users}/${min_likes_received_users}\n`;
    

    it's awesome!


  • sockdevs

    In a world without format strings, I guess it is pretty useful


  • sockdevs

    @RaceProUK said:

    In a world without format strings, I guess it is pretty useful

    i have many times written a format string function for JS

    often it looks like

    function formatbetter(str, fmt){
        for(var name in fmt) {
            str = str.replace('%'name+'%', fmt[name]);
        }
        return fmt;
    }
    

    having support for that (albeit with different syntax) is a nice win for me, It means that i can do proper formatting without worrying that one can craft a replacement string that replaces another replacement string and causes mass havoc!


  • sockdevs

    Don't get me wrong; having read the full details of the feature, it is indeed a good one. The best bits of string interpolation and verbatim strings put together. I just couldn't resist having a dig at both JS and PHP, given their general :wtf:iness ;)



  • @loopback0 said:

    This was the result I found.

    I looked at it. There was a jsfiddle with text in the other answer, so I played with it a little. And I found a (to me) surprising thing. When the HTML contains line breaks, they are rendered as spaces (except in <pre> of course). But when innerText (or innerHTML) gets a newline appended, it gets converted to <br/> and actual newline appears.

    And I'd like to ask the resident JavaScripters, is that by specification or by misguided kindness of V8 implementors?

    The modified jsfiddle is here.



  • the thing is. you want to control the format of that %varType to string conversion.
    and without a way to specify it, the templating capabilities are really limited


  • sockdevs

    That's a point; that's one of the best things about C# format strings, the format specifiers


  • sockdevs

    @Bulb said:

    is that by specification

    nope.

    @Bulb said:

    misguided kindness of V8 implementors?

    also nope.

    that abomination is the result of the folks at redmond.


  • sockdevs

    @accalia said:

    that abomination is the result of the folks at redmond

    Which has been allowed to spread, thanks to Opera and WebKit.

    At least Mozilla have retained sense.



  • @RaceProUK said:

    At least Mozilla have retained sense.

    Had to happen eventually.


  • Winner of the 2016 Presidential Election

    talk about SO and javascript in the General category (not Coding Help) without any local JS dev trying to propose the worst possible, most convoluted, jaw dropping jQuery-solution?

    Come on, you guys can do better than that!

    Filed Under: Son/Daughter/Pet, I am disapoint!


  • sockdevs

    @Kuro said:

    Pet

    I am no-one's pet!

    Well, when I say 'no-one', there is one exception. But then she's also my pet, so it balances out :smile:



  • @Kuro said:

    talk about SO and javascript in the General category (not Coding Help)

    I didn't need Coding Help when I created the topic ;)


  • Winner of the 2016 Presidential Election

    @RaceProUK said:

    I am no-one's pet!

    I also don't have children, yet.... So I think that balances everything out!

    Filed Under: It's all about perspective or something!


    @loopback0 said:

    I didn't need Coding Help

    Just a precaution before this gets moved and somebody complains that I suggested suggesting a terrible jQuery-answer!

    Filed Under: You can never be safe enough around here



  • there's a plugin for that!



  • @powerlord said:

    Also, is the \n thing broken because you used ' instead of "? I forget if that makes a difference in JS liike it does in PHP.

    It makes a difference kinda.

    If the outer quote is " you can use ' as your inner quote. And vice-versa. So if you want a string containing normal-looking English quotes, you're better off using a ' to denote the string, then you can just type like normal inside it,

    If you're not using an editor with syntax coloring to help you out with this, well. Don't do that.



  • @Bulb said:

    And I'd like to ask the resident JavaScripters, is that by specification or by misguided kindness of V8 implementors?

    V8's a JS engine. You're describing a DOM feature. So... regardless of whether it's intentional, it has nothing to do with V8.

    Yes, this is Yet Another Blakeyrat Sick Of People Conflating JS And DOM Post. (Protip: if it's poorly-designed and stupid, it's probably DOM's fault.)



  • @blakeyrat said:

    (Protip: if it's poorly-designed and stupid, it's probably DOM's fault.)
    My opinion of JS has improved a fair bit from what it used to be, but still... JS itself puts up a pretty damn good fight in the "poorly-designed and stupid" competition. It has plenty to trot out there.



  • @RaceProUK said:

    @accalia said:
    other than that there's no difference. now the new template strings in ES6 are a whole new beast and i want them in nodeJS because :heart_eyes_cat:

    Because what JS really needs is a feature from PHP :laughing:

    It's worse than that. Judging from @accalia's example, its syntax looks suspiciously like Java's JSP Expression Language (EL).


  • sockdevs

    @EvanED said:

    JS itself puts up a pretty damn good fight in the "poorly-designed and stupid" competition

    Only three things spring to mind:

    1. Type coercion; sometimes feels too lenient
    2. Automatic creation of variables; call me crazy, but that's just asking for bugs (and it's hit me before)
    3. Automatic semicolon insertion; can you guarantee it'll always be right?

    Can't really think of anything else right now.


  • sockdevs

    @EvanED said:

    My opinion of JS has improved a fair bit from what it used to be, but still... JS itself puts up a pretty damn good fight in the "poorly-designed and stupid" competition. It has plenty to trot out there.

    there are bad parts of it (discounting DOM because you have to discount DOM) but overall they're fairly obvious once you'v ebeen working with it for a while.

    i recommend pickung up a copy of this book:

    i might not be a fan of how dogmatic Douglas Crockford (and by extension jslint) are at enforcing some frankly rather arbitrary style rules, but overall the book is an excellent way to find the good parts of JS and work with them. And of course finding a good lint tool (i recommend ESlint, it's fantastic) is invaluable, no matter what the language is.



  • @RaceProUK said:

    At least Mozilla have retained sense.

    In the spirit of the gold topics, if everyone else did something different, is this really rational or sensical?


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.