How to stop babel from prefixing “_” for methodnames containing same name as javascript keywords












1















I am transforming a class from es6 to es5 using babel transpiler.
This is the sample class.



class TestClass{
switch(){
}
getState(){

}
}


This gets transformed as



 var TestClass = function () {
function TestClass() {
_classCallCheck(this, TestClass);
}

_createClass(TestClass, [{
key: "switch",
value: function _switch() {}
}, {
key: "getState",
value: function getState() {}
}]);

return TestClass;
}();


If you notice here, switch method is automatically prefixed with "_" by babel. How to avoid this?



Here is the babel transformation demo










share|improve this question

























  • What do you expect the transpiler to do, if you used a reserved word? Creating invalid JavaScript?

    – ssc-hrep3
    Nov 19 '18 at 7:26
















1















I am transforming a class from es6 to es5 using babel transpiler.
This is the sample class.



class TestClass{
switch(){
}
getState(){

}
}


This gets transformed as



 var TestClass = function () {
function TestClass() {
_classCallCheck(this, TestClass);
}

_createClass(TestClass, [{
key: "switch",
value: function _switch() {}
}, {
key: "getState",
value: function getState() {}
}]);

return TestClass;
}();


If you notice here, switch method is automatically prefixed with "_" by babel. How to avoid this?



Here is the babel transformation demo










share|improve this question

























  • What do you expect the transpiler to do, if you used a reserved word? Creating invalid JavaScript?

    – ssc-hrep3
    Nov 19 '18 at 7:26














1












1








1








I am transforming a class from es6 to es5 using babel transpiler.
This is the sample class.



class TestClass{
switch(){
}
getState(){

}
}


This gets transformed as



 var TestClass = function () {
function TestClass() {
_classCallCheck(this, TestClass);
}

_createClass(TestClass, [{
key: "switch",
value: function _switch() {}
}, {
key: "getState",
value: function getState() {}
}]);

return TestClass;
}();


If you notice here, switch method is automatically prefixed with "_" by babel. How to avoid this?



Here is the babel transformation demo










share|improve this question
















I am transforming a class from es6 to es5 using babel transpiler.
This is the sample class.



class TestClass{
switch(){
}
getState(){

}
}


This gets transformed as



 var TestClass = function () {
function TestClass() {
_classCallCheck(this, TestClass);
}

_createClass(TestClass, [{
key: "switch",
value: function _switch() {}
}, {
key: "getState",
value: function getState() {}
}]);

return TestClass;
}();


If you notice here, switch method is automatically prefixed with "_" by babel. How to avoid this?



Here is the babel transformation demo







javascript ecmascript-6 babeljs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 7:27









Felix Kling

547k126849909




547k126849909










asked Nov 19 '18 at 7:20









RohiniRohini

6712




6712













  • What do you expect the transpiler to do, if you used a reserved word? Creating invalid JavaScript?

    – ssc-hrep3
    Nov 19 '18 at 7:26



















  • What do you expect the transpiler to do, if you used a reserved word? Creating invalid JavaScript?

    – ssc-hrep3
    Nov 19 '18 at 7:26

















What do you expect the transpiler to do, if you used a reserved word? Creating invalid JavaScript?

– ssc-hrep3
Nov 19 '18 at 7:26





What do you expect the transpiler to do, if you used a reserved word? Creating invalid JavaScript?

– ssc-hrep3
Nov 19 '18 at 7:26












1 Answer
1






active

oldest

votes


















3














You didn't test it first. There is nothing wrong with Babel. The _switch() is naught but an internal unexposed method that Babel creates during transpile (something you shouldn't really care about). Your accessible method is still called .switch() as all things should be.



class TestClass{
switch(){
return "foo"
}
getState(){

}
}

console.log(new TestClass().switch()) //foo
console.log(new TestClass()._switch()) //undefined





"use strict";

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var TestClass = function () {
function TestClass() {
_classCallCheck(this, TestClass);
}

_createClass(TestClass, [{
key: "switch",
value: function _switch() {
return "foo";
}
}, {
key: "getState",
value: function getState() {}
}]);

return TestClass;
}();

console.log(new TestClass().switch())








share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53369968%2fhow-to-stop-babel-from-prefixing-for-methodnames-containing-same-name-as-jav%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    You didn't test it first. There is nothing wrong with Babel. The _switch() is naught but an internal unexposed method that Babel creates during transpile (something you shouldn't really care about). Your accessible method is still called .switch() as all things should be.



    class TestClass{
    switch(){
    return "foo"
    }
    getState(){

    }
    }

    console.log(new TestClass().switch()) //foo
    console.log(new TestClass()._switch()) //undefined





    "use strict";

    var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

    function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

    var TestClass = function () {
    function TestClass() {
    _classCallCheck(this, TestClass);
    }

    _createClass(TestClass, [{
    key: "switch",
    value: function _switch() {
    return "foo";
    }
    }, {
    key: "getState",
    value: function getState() {}
    }]);

    return TestClass;
    }();

    console.log(new TestClass().switch())








    share|improve this answer






























      3














      You didn't test it first. There is nothing wrong with Babel. The _switch() is naught but an internal unexposed method that Babel creates during transpile (something you shouldn't really care about). Your accessible method is still called .switch() as all things should be.



      class TestClass{
      switch(){
      return "foo"
      }
      getState(){

      }
      }

      console.log(new TestClass().switch()) //foo
      console.log(new TestClass()._switch()) //undefined





      "use strict";

      var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

      function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

      var TestClass = function () {
      function TestClass() {
      _classCallCheck(this, TestClass);
      }

      _createClass(TestClass, [{
      key: "switch",
      value: function _switch() {
      return "foo";
      }
      }, {
      key: "getState",
      value: function getState() {}
      }]);

      return TestClass;
      }();

      console.log(new TestClass().switch())








      share|improve this answer




























        3












        3








        3







        You didn't test it first. There is nothing wrong with Babel. The _switch() is naught but an internal unexposed method that Babel creates during transpile (something you shouldn't really care about). Your accessible method is still called .switch() as all things should be.



        class TestClass{
        switch(){
        return "foo"
        }
        getState(){

        }
        }

        console.log(new TestClass().switch()) //foo
        console.log(new TestClass()._switch()) //undefined





        "use strict";

        var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

        function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

        var TestClass = function () {
        function TestClass() {
        _classCallCheck(this, TestClass);
        }

        _createClass(TestClass, [{
        key: "switch",
        value: function _switch() {
        return "foo";
        }
        }, {
        key: "getState",
        value: function getState() {}
        }]);

        return TestClass;
        }();

        console.log(new TestClass().switch())








        share|improve this answer















        You didn't test it first. There is nothing wrong with Babel. The _switch() is naught but an internal unexposed method that Babel creates during transpile (something you shouldn't really care about). Your accessible method is still called .switch() as all things should be.



        class TestClass{
        switch(){
        return "foo"
        }
        getState(){

        }
        }

        console.log(new TestClass().switch()) //foo
        console.log(new TestClass()._switch()) //undefined





        "use strict";

        var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

        function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

        var TestClass = function () {
        function TestClass() {
        _classCallCheck(this, TestClass);
        }

        _createClass(TestClass, [{
        key: "switch",
        value: function _switch() {
        return "foo";
        }
        }, {
        key: "getState",
        value: function getState() {}
        }]);

        return TestClass;
        }();

        console.log(new TestClass().switch())








        "use strict";

        var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

        function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

        var TestClass = function () {
        function TestClass() {
        _classCallCheck(this, TestClass);
        }

        _createClass(TestClass, [{
        key: "switch",
        value: function _switch() {
        return "foo";
        }
        }, {
        key: "getState",
        value: function getState() {}
        }]);

        return TestClass;
        }();

        console.log(new TestClass().switch())





        "use strict";

        var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

        function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

        var TestClass = function () {
        function TestClass() {
        _classCallCheck(this, TestClass);
        }

        _createClass(TestClass, [{
        key: "switch",
        value: function _switch() {
        return "foo";
        }
        }, {
        key: "getState",
        value: function getState() {}
        }]);

        return TestClass;
        }();

        console.log(new TestClass().switch())






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 19 '18 at 7:30

























        answered Nov 19 '18 at 7:24









        Abana ClaraAbana Clara

        1,561819




        1,561819






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53369968%2fhow-to-stop-babel-from-prefixing-for-methodnames-containing-same-name-as-jav%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to send String Array data to Server using php in android

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Is anime1.com a legal site for watching anime?