Javascript 将 this 传递给函数

标签 javascript variables this

我不习惯使用 this 并尝试让一些简单的函数来回传递它。我不太确定 javascript 期望什么,但我认为我做的不对。

$(".search-result").each(function() {
    var x  = $(this).find('.past-positions ol').children()
    console.log(x)
    //this prints as expected
    pastJobs(this)
    // this does not
})

function pastJobs() {
    var x  = $(this).find('.past-positions ol').children()
    console.log(x)
    // this prints as undefined
}

我认为可以将 this 传递给函数,但我认为我的做法不对。

我做错了什么?

最佳答案

改为尝试 pastJobs.call(this)

关于Javascript 将 this 传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55325271/

相关文章:

javascript - 来自带有缩略图的 JS 数组的 Jquery 或 JavaScript 图像 slider

javascript - ColumnChart 不显示所有字符串标签

PHP 对象在变量名中有分号(或其他奇怪的字符)

javascript - 鼠标悬停时淡入/淡出图片

javascript - 如何使用构造函数中创建的方法来更新同一构造函数中的另一个变量?

javascript - 带有 querySelectorAll 的 lightgallery.js

javascript - 是否可以将传单版本 1 与自定义 mapbox 样式一起使用?

php - 为什么我的表单变量没有通过 POST 传递?

R 到 Stata : Exporting dataframe with variable labels

javascript - 在工厂函数中,对象名称和 'this' 关键字是否经常或总是可以互换?