最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Function converted to arrow function - function not defined - Stack Overflow

programmeradmin2浏览0评论

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), while getPage().filename (probably) isn't. – nbokmans Commented Jan 7, 2019 at 13:19
  • 1 While dealing with arrow functions, you have to define getPage first then call it. Otherwise it will throw error – Karan Commented Jan 7, 2019 at 13:23
Add a ment  | 

4 Answers 4

Reset to default 3

It'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

发布评论

评论列表(0)

  1. 暂无评论