gpt4 book ai didi

javascript - 如何在对象的属性值中引用 'this'?

转载 作者:行者123 更新时间:2023-11-27 22:40:41 26 4
gpt4 key购买 nike

var search = {
form: $('search-form'),
input: this.form.find('.search-input'), // 'this' undefined
callback: function(){
console.log(this.form) // 'this' not undefined
}
}

请帮帮我!抱歉,我的英语不是很好,但我会尽力解释我的问题。我无法选择当前对象表单内的输入元素,因为 this.form 未定义。为什么它是未定义的?

这段代码有什么问题?

input: this.form.find('.search-input')

我该如何更正上面的代码?

最佳答案

您可以将 input 属性更改为函数:

var search = {
form: $('search-form'),
input: function() {
return this.form.find('.search-input')
},
callback: function(){
console.log(this.form) // 'this' not undefined
}
}

然后使用search.input()调用来获取结果。

关于javascript - 如何在对象的属性值中引用 'this'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38765467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com