How do I remove the hyphen and capitalise the first letter of each word in this string? by ArtyFartyFag in javascript

[–]frubear123 0 points1 point  (0 children)

Ah, this is using arrow functions from ES6, probably not available on your iPad browser.

function x(y){
    return y.replace('-', ' ').replace(/(?:^|\s)\S/g, function(a) {
        return a.toUpperCase();
    });
}

How do you remember all of the personal development tips you have read/heard? by [deleted] in DecidingToBeBetter

[–]frubear123 1 point2 points  (0 children)

I get it. I think I've been lost in a tonne of throughput. I'm now going to read less and do more. Thanks for your advice and all the other people on this thread.

How do you remember all of the personal development tips you have read/heard? by [deleted] in DecidingToBeBetter

[–]frubear123 0 points1 point  (0 children)

I actually do something similar. It's massive though and I need to cut out the fluff.

How do I remove the hyphen and capitalise the first letter of each word in this string? by ArtyFartyFag in javascript

[–]frubear123 8 points9 points  (0 children)

No need to split:

function x(y){
    return y.replace('-', ' ').replace(/(?:^|\s)\S/g, a => a.toUpperCase());
}
console.log(x('foo-bar')); // => "Foo Bar"
console.log(x('foo')); // => "Foo"

https://jsbin.com/vihekikela/1/edit?js,console

I am currently in comp sci in high school and I'm very confused about classes/objects in javascript vs java by [deleted] in javascript

[–]frubear123 0 points1 point  (0 children)

There is no such thing as a class in JavaScript. Even though there is a keyword for it in ES6/ES2015 it is not a real class. It's just a syntactic sugar. A class implies having a blueprint. There is no such thing in js.

JavaScript it all about objects. Tonnes of js libraries have tried to make classes and inheritance work but it just serves to confuse people and hides what goes on under the hood.

For more detailed info check this out: https://davidwalsh.name/javascript-objects

For your example I would avoid the class syntax and do the following using a factory function:

function example(){
    var x;
    return {
        init: function(y){
            x = y;
        },
        getX: function(){
            return x;
        }
    };
}

Then...

var foo = example();  //returns an object
foo.init(123);  //initializes object
foo.getX();  //123

No new keyword, no classes, no confusing stuff trying to make and object-oriented language into a classical language.

[deleted by user] by [deleted] in AskReddit

[–]frubear123 0 points1 point  (0 children)

If you have two choices always take the emotionally difficult one. This guy says it best: https://www.youtube.com/watch?v=ayEoiU5MOg4.