Bare words in JavaScript

    Once relaxing on the couch, I remembered a fleeting wonderful movie from my childhood, which was called WAT . And there was such a thing as bare words, which was shown on the example of Ruby.

    image

    Bare words are words without quotes that you can use as strings.

    In JavaScript, they seem to be absent, so I tried to push them into it, which in general turned out.

    The result is this code:

    with(bareWords) {
        alert(Иван + Ургант)
        console.log(We, can, use, bare, words)
    }
    
    And he successfully displayed the necessary phrases.
    And it was implemented like this:

    try {
        let self = this
        window.bareWords = new Proxy({}, {
            has: function(target, name) {
                return !(name in self)
            },
            get: function(target, name) {
                return name
            },
        })
    } catch(e) {
        console.error('Your browser doesn\'t support bare words.')
    }
    

    As you can see, so that we can use bare words, we used such features of the javascript as Proxy and with .

    When trying to get a property from this, due to the intrigues with, the request goes to the proxy, where we return a string value instead of a vicious error about the absence of a variable.

    Thanks for attention.

    image

    If anything, here's a link to Github

    Also popular now: