How to stop babel from prefixing “_” for methodnames containing same name as javascript keywords
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
add a comment |
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
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
add a comment |
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
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
javascript ecmascript-6 babeljs
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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())add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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())add a comment |
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())add a comment |
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())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())edited Nov 19 '18 at 7:30
answered Nov 19 '18 at 7:24
Abana ClaraAbana Clara
1,561819
1,561819
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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