[英]Extending js prototype, argument is getting type coerced
I have a function that start's like this: 我有一个像这样的函数:
Object.prototype.search=function(str){
var o=this;
var keys=Object.keys(o);
var obj={};
str=str.toLowerCase();
var regEx=new RegExp(str,"ig");
... etc.
Note - this is just a quick and dirty function for searching through some data. 注意-这只是用于搜索某些数据的快速而肮脏的功能。 I'm sure what I want could be achieved more effectively, but I just wanted to understand something, etc..
我确定可以更有效地实现我想要的东西,但是我只是想了解一些东西。
I created the rest of the function, and on my first try in the console, I happened to pass it a number. 我创建了该函数的其余部分,并且在我第一次尝试在控制台中时,碰巧给它传递了一个数字。 Now the number is passed as a string, and I want it to be a string, and when I run
"555".toLowerCase()
in the console, no problem, returns string unchanged as expected. 现在,该数字作为字符串传递,我希望它是一个字符串,当我在控制台中运行
"555".toLowerCase()
时,没问题,按预期返回字符串。 However, when I try and execute the function, it throws an exception: 但是,当我尝试执行该函数时,它将引发异常:
uncaught TypeError: str.toLowerCase is not a function
at Number.Object.search
So I can see that it's converting my string into a number, but I can't understand why. 所以我可以看到它正在将我的字符串转换为数字,但是我不明白为什么。 Is it because I'm working on a built-in object?
是因为我正在处理内置对象吗? The error is thrown on the line where I attempt
str=str.toLowerCase()
. 错误在我尝试
str=str.toLowerCase()
的行上引发。 Seems like the solution would be to explicitly declare it as a string, but I can see why I need to. 似乎解决方案是将其显式声明为字符串,但是我明白了为什么需要这样做。
Any ideas? 有任何想法吗?
Edit: Here is my code - I know it's yucky, but now I'm having other issues, and I'm just curious, because I can't figure out what's going on: 编辑:这是我的代码-我知道这很麻烦,但是现在我遇到了其他问题,我很好奇,因为我不知道发生了什么事情:
Object.prototype.search=function(str){
const o=this;
var keys=Object.keys(o);
var values=Object.values(o);
var obj={};
var regex=new RegExp(str.toLowerCase(),"ig");
values=values.map((value,index)=>{
if(value){
value=value.toString();
return value.search(regex) !==-1 ? index : false;
}
return false;
}).filter(e=>!!e);
values.forEach((e,i)=>{
var key=keys[i];
var value=o[key];
obj[key]=value;
});
return obj;
};
Edit: And here is it in action: 编辑:这是在行动:
var x={"cat":"23","hat":"45"};
x.search("2");
Expected result: {"cat":"23"};
Not: After I included the null check for value, I'm no longer getting an error, but I'm also not getting the results I'd expect. 不:在包括了对值的空检查之后,我不再得到错误,但是我也没有得到我期望的结果。 In other words, I'm getting an object back, and it's filtered, but it's returning the "wrong" values given the argument passed.
换句话说,我得到了一个对象,并对其进行了过滤,但是根据给定的参数,它返回的是“错误”的值。
Edit: Okay - it is now fixed. 编辑:好的-现在已修复。 The problem was in this line:
var key=keys[i];
问题出在这一行:
var key=keys[i];
. 。 I was using the index of the keys instead of the index of the values.
我正在使用键的索引而不是值的索引。 So now its right.
所以现在是对的。 But I still can't understand why I was getting the error I was prior to the null check.
但是我仍然不明白为什么我会得到空检查之前的错误。 I'll close this if people think I should, but I still don't understand why I was getting the error.
如果人们认为我应该关闭该窗口,但是我仍然不明白为什么会出现错误。
Cleaner version of your function: 功能的更清洁版本:
Object.defineProperty(Object.prototype, 'search', { value: function(regex) { let found = {}; for (let k in this) { if (this.hasOwnProperty(k) && regex.test(this[k].toString())) found[k] = this[k]; } return found; }, enumerable: false }); console.log(({ cat: 23, hat: 45 }).search(/2/)); console.log(({ cat: 23, hat: 45 }).search(/.*/));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.