admin管理员组文章数量:1023827
I'm just learning ES6 and learned about arrow functions. I'm going through an existing file and converting functions one by one. I've converted numerous functions and all have worked as they did previously with the exception of 1.
Doing this, I can call page
to get the current filename
let textArr = getPage.textArr;
let headingArr = getPage.headingArr;
const page = getPage.filename;
function getPage() {
const url = window.location.pathname,
fileWithExtension = url.substring(url.lastIndexOf('/')+1),
filename = fileWithExtension.split('.').slice(0, -1).join('.')
;
if (filename == "foo") {
textArr = [
`Text`,
`Text`
];
headingArr = null;
return {filename, textArr, headingArr}
}
}
Leaving everything as is, converting to an arrow function, then calling page
in console, I get:
getPage is not defined at line 1
const getPage = () => {
const url = window.location.pathname,
fileWithExtension = url.substring(url.lastIndexOf('/')+1),
filename = fileWithExtension.split('.').slice(0, -1).join('.')
;
if (filename == "foo") {
textArr = [
`Text`,
`Text`
];
headingArr = null;
return {filename, textArr, headingArr}
}
}
I'm just learning ES6 and learned about arrow functions. I'm going through an existing file and converting functions one by one. I've converted numerous functions and all have worked as they did previously with the exception of 1.
Doing this, I can call page
to get the current filename
let textArr = getPage.textArr;
let headingArr = getPage.headingArr;
const page = getPage.filename;
function getPage() {
const url = window.location.pathname,
fileWithExtension = url.substring(url.lastIndexOf('/')+1),
filename = fileWithExtension.split('.').slice(0, -1).join('.')
;
if (filename == "foo") {
textArr = [
`Text`,
`Text`
];
headingArr = null;
return {filename, textArr, headingArr}
}
}
Leaving everything as is, converting to an arrow function, then calling page
in console, I get:
getPage is not defined at line 1
const getPage = () => {
const url = window.location.pathname,
fileWithExtension = url.substring(url.lastIndexOf('/')+1),
filename = fileWithExtension.split('.').slice(0, -1).join('.')
;
if (filename == "foo") {
textArr = [
`Text`,
`Text`
];
headingArr = null;
return {filename, textArr, headingArr}
}
}
Share
Improve this question
edited Jan 7, 2019 at 13:42
froggomad
asked Jan 7, 2019 at 13:17
froggomadfroggomad
1,9353 gold badges21 silver badges44 bronze badges
3
- 2 You need to have the balanced bracket. You are missing one closing bracket in the posted question. – Hassan Imam Commented Jan 7, 2019 at 13:19
-
getPage
is a function, not a property. Call the function.getPage.filename
is undefined (as per your first code block), whilegetPage().filename
(probably) isn't. – nbokmans Commented Jan 7, 2019 at 13:19 -
1
While dealing with
arrow functions
, you have to definegetPage
first then call it. Otherwise it will throw error – Karan Commented Jan 7, 2019 at 13:23
4 Answers
Reset to default 3It's a hoisting problem. Functions are hoisted by default. That is not happening to arrow functions assigned to a variable. If you want that to work, you need to move the const definition before you call it. Like this:
const getPage = () => {
const url = window.location.pathname,
fileWithExtension = url.substring(url.lastIndexOf('/')+1),
filename = fileWithExtension.split('.').slice(0, -1).join('.')
;
if (filename == "foo") {
textArr = [
`Text`,
`Text`
];
headingArr = null;
return {filename, textArr, headingArr}
}
}
let textArr = getPage().textArr;
let headingArr = getPage().headingArr;
const page = getPage().filename;
You are actually not calling your function, i.e. const page = getPage.filename;
should be replaced by const page = getPage().filename;
For all three variables you could do something like:
let {textArr, headingArr, textArr} = getPage();
Add 1 more braces then it will work.
const getPage = () => {
const url = window.location.pathname,
fileWithExtension = url.substring(url.lastIndexOf('/')+1),
filename = fileWithExtension.split('.').slice(0, -1).join('.');
if (filename == "foo") {
textArr = [
`Text`,
`Text`
];
headingArr = null;
return {filename, textArr, headingArr}
}
}
If you run into this problem and you're using jQuery
, remove the function definition from $(document).ready()
function.
Regards
I'm just learning ES6 and learned about arrow functions. I'm going through an existing file and converting functions one by one. I've converted numerous functions and all have worked as they did previously with the exception of 1.
Doing this, I can call page
to get the current filename
let textArr = getPage.textArr;
let headingArr = getPage.headingArr;
const page = getPage.filename;
function getPage() {
const url = window.location.pathname,
fileWithExtension = url.substring(url.lastIndexOf('/')+1),
filename = fileWithExtension.split('.').slice(0, -1).join('.')
;
if (filename == "foo") {
textArr = [
`Text`,
`Text`
];
headingArr = null;
return {filename, textArr, headingArr}
}
}
Leaving everything as is, converting to an arrow function, then calling page
in console, I get:
getPage is not defined at line 1
const getPage = () => {
const url = window.location.pathname,
fileWithExtension = url.substring(url.lastIndexOf('/')+1),
filename = fileWithExtension.split('.').slice(0, -1).join('.')
;
if (filename == "foo") {
textArr = [
`Text`,
`Text`
];
headingArr = null;
return {filename, textArr, headingArr}
}
}
I'm just learning ES6 and learned about arrow functions. I'm going through an existing file and converting functions one by one. I've converted numerous functions and all have worked as they did previously with the exception of 1.
Doing this, I can call page
to get the current filename
let textArr = getPage.textArr;
let headingArr = getPage.headingArr;
const page = getPage.filename;
function getPage() {
const url = window.location.pathname,
fileWithExtension = url.substring(url.lastIndexOf('/')+1),
filename = fileWithExtension.split('.').slice(0, -1).join('.')
;
if (filename == "foo") {
textArr = [
`Text`,
`Text`
];
headingArr = null;
return {filename, textArr, headingArr}
}
}
Leaving everything as is, converting to an arrow function, then calling page
in console, I get:
getPage is not defined at line 1
const getPage = () => {
const url = window.location.pathname,
fileWithExtension = url.substring(url.lastIndexOf('/')+1),
filename = fileWithExtension.split('.').slice(0, -1).join('.')
;
if (filename == "foo") {
textArr = [
`Text`,
`Text`
];
headingArr = null;
return {filename, textArr, headingArr}
}
}
Share
Improve this question
edited Jan 7, 2019 at 13:42
froggomad
asked Jan 7, 2019 at 13:17
froggomadfroggomad
1,9353 gold badges21 silver badges44 bronze badges
3
- 2 You need to have the balanced bracket. You are missing one closing bracket in the posted question. – Hassan Imam Commented Jan 7, 2019 at 13:19
-
getPage
is a function, not a property. Call the function.getPage.filename
is undefined (as per your first code block), whilegetPage().filename
(probably) isn't. – nbokmans Commented Jan 7, 2019 at 13:19 -
1
While dealing with
arrow functions
, you have to definegetPage
first then call it. Otherwise it will throw error – Karan Commented Jan 7, 2019 at 13:23
4 Answers
Reset to default 3It's a hoisting problem. Functions are hoisted by default. That is not happening to arrow functions assigned to a variable. If you want that to work, you need to move the const definition before you call it. Like this:
const getPage = () => {
const url = window.location.pathname,
fileWithExtension = url.substring(url.lastIndexOf('/')+1),
filename = fileWithExtension.split('.').slice(0, -1).join('.')
;
if (filename == "foo") {
textArr = [
`Text`,
`Text`
];
headingArr = null;
return {filename, textArr, headingArr}
}
}
let textArr = getPage().textArr;
let headingArr = getPage().headingArr;
const page = getPage().filename;
You are actually not calling your function, i.e. const page = getPage.filename;
should be replaced by const page = getPage().filename;
For all three variables you could do something like:
let {textArr, headingArr, textArr} = getPage();
Add 1 more braces then it will work.
const getPage = () => {
const url = window.location.pathname,
fileWithExtension = url.substring(url.lastIndexOf('/')+1),
filename = fileWithExtension.split('.').slice(0, -1).join('.');
if (filename == "foo") {
textArr = [
`Text`,
`Text`
];
headingArr = null;
return {filename, textArr, headingArr}
}
}
If you run into this problem and you're using jQuery
, remove the function definition from $(document).ready()
function.
Regards
本文标签:
版权声明:本文标题:javascript - Function converted to arrow function - function not defined - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745539194a2155101.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论