通过函数传递“this”时,我是否可以无法访问“this”的属性
Can I not access attributes of 'this' when passing `this` through a function?
注意:整个问题基于问题是语法错误的场景;传递"this"实际上没有问题。这个问题应该结束了。
例如,以下工作:
$('.base-icons').click(function () {
selectedIcon($(this).attr("src").split(/'/('/*)/));
});
var selectedIcon = function(myObj) {
console.log(myObj);
};
并按预期打印缩短的字符串。以下方法不起作用:
$('.base-icons').click(function () {
selectedIcon(this);
});
var selectedIcon = function(myObj) {
console.log($(myObj).attr("src").split(/'/('/*)/)[6]);
};
因为它打印undefined
.为什么?谢谢。
例如,以下工作:
selectedIcon($(this).attr("src").split(/'/('/!*)/)[6]) var selectedIcon = function(myObj) { console.log(myObj) }
并按预期打印缩短的字符串。以下方法不起作用:
selectedIcon(this) var selectedIcon = function(myObj) { console.log($(myObj).attr("src").split(/'/('/!*)/)[6]) }
因为它打印未定义。为什么?谢谢。
在javascript
提问时selectedIcon
undefined
,导致TypeError
问题中的两个示例都应返回错误。
Uncaught TypeError: selectedIcon is not a function
window.onerror = function(e) {
console.info(e);
alert(e);
}
selectedIcon("abc");
var selectedIcon = function (myObj) {
alert(myObj)
}
是的,您可以在通过函数传递this
时访问this
的属性。
您可以像这样访问此属性。
$(this).attr("href");
相关文章:
- javascript中对象构造函数中的var属性与this.properties
- 如何使用(this)访问Angular 2 http rxjs catch函数中的对象属性
- 为什么我可以'不要使用xx[this.index].style.display?它's说“;无法读取属性
- ember.js计算属性中this的值是如何定义的
- $(this) 在 onclick 属性中不起作用
- jQuery插件返回this.each并为每个对象添加函数属性
- 使用 $(this) 的属性
- 通过函数传递“this”时,我是否可以无法访问“this”的属性
- Jquery $this属性选择器是否包含单词
- Javascript对象,其属性是有关“this”的函数详细信息
- 为什么您可以在不引用“this”的情况下操作对象属性
- jQuery - 如何在 jquery 中访问 “this.options” dataSousrce 属性
- 如何使用“self = this”访问类属性
- JavaScript 中对象的属性使用 this 和不使用 this
- 在 Javascript 中,在执行深度复制时,由于属性为“this”,我如何避免循环
- 在 JavaScript/TypeScript 中动态地“直接”添加对象属性到“this”
- 对象中的函数和对“this”属性的访问 - 失败
- XmlHttpRequest加载' this '属性
- 多个$this属性用于JS中可重用的类
- 如何在jQuery中选择与this属性匹配的所有元素