箭头语法和函数语法真的一样吗? (无法读取未定义的 属性 'createDocumentFragment')
Are Arrow syntax and function syntax really the same? (Cannot read property 'createDocumentFragment' of undefined)
所以我的代码发生了一件非常有趣的事情。使用箭头语法声明我的函数时出现错误。但是当我使用旧的 function() 语法时没有错误。
const clean = (blurb) => {
blurb.find('a').each(()=> { $(this).replaceWith($(this).html()) });
}
(无法读取未定义的 属性 'createDocumentFragment')
^^^^错误^^^^
对
const clean = (blurb) => {
blurb.find('a').each(function() { $(this).replaceWith($(this).html()) });
}
^^^^没有错误^^^^
如此干净应该只删除 'a' 标签的所有实例,但这只在我使用旧方法时有效?知道为什么会这样吗?
谢谢
箭头函数不像常规函数那样有自己的 this
,这可能是您出错的原因。
更多信息:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
所以我的代码发生了一件非常有趣的事情。使用箭头语法声明我的函数时出现错误。但是当我使用旧的 function() 语法时没有错误。
const clean = (blurb) => {
blurb.find('a').each(()=> { $(this).replaceWith($(this).html()) });
}
(无法读取未定义的 属性 'createDocumentFragment')
^^^^错误^^^^
对
const clean = (blurb) => {
blurb.find('a').each(function() { $(this).replaceWith($(this).html()) });
}
^^^^没有错误^^^^
如此干净应该只删除 'a' 标签的所有实例,但这只在我使用旧方法时有效?知道为什么会这样吗?
谢谢
箭头函数不像常规函数那样有自己的 this
,这可能是您出错的原因。
更多信息:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions