Extending js prototype, argument is getting type coerced2019 Community Moderator ElectionHow to define method in javascript on Array.prototype and Object.prototype so that it doesn't appear in for in loopWhat's the canonical way to check for type in Python?How do you get a timestamp in JavaScript?How to get the children of the $(this) selector?Get the name of an object's typeHow to determine a Python variable's type?How does JavaScript .prototype work?How can I get query string values in JavaScript?Get the current URL with JavaScript?How do I pass command line arguments to a Node.js program?What are the nuances of scope prototypal / prototypical inheritance in AngularJS?

Weird lines in Microsoft Word

Jem'Hadar, something strange about their life expectancy

Should a narrator ever describe things based on a characters view instead of fact?

What is it called when someone votes for an option that's not their first choice?

Nested Dynamic SOQL Query

Why is participating in the European Parliamentary elections used as a threat?

Why didn’t Eve recognize the little cockroach as a living organism?

Have any astronauts/cosmonauts died in space?

Why does Surtur say that Thor is Asgard's doom?

Writing in a Christian voice

UK Tourist Visa- Enquiry

Imaginary part of expression too difficult to calculate

How to balance a monster modification (zombie)?

How to read string as hex number in bash?

Homology of the fiber

How do you justify more code being written by following clean code practices?

Exposing a company lying about themselves in a tightly knit industry: Is my career at risk on the long run?

Have the tides ever turned twice on any open problem?

Why are there no stars visible in cislunar space?

Friend wants my recommendation but I don't want to

Hot air balloons as primitive bombers

Is there any common country to visit for uk and schengen visa?

Do native speakers use "ultima" and "proxima" frequently in spoken English?

CLI: Get information Ubuntu releases



Extending js prototype, argument is getting type coerced



2019 Community Moderator ElectionHow to define method in javascript on Array.prototype and Object.prototype so that it doesn't appear in for in loopWhat's the canonical way to check for type in Python?How do you get a timestamp in JavaScript?How to get the children of the $(this) selector?Get the name of an object's typeHow to determine a Python variable's type?How does JavaScript .prototype work?How can I get query string values in JavaScript?Get the current URL with JavaScript?How do I pass command line arguments to a Node.js program?What are the nuances of scope prototypal / prototypical inheritance in AngularJS?










0















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. 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(). 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];. 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.










share|improve this question



















  • 1





    What does the rest of your code look like?

    – zynth666
    Mar 7 at 18:57






  • 1





    Please include examples of how you're calling Object.prototype.search

    – Gershom Maes
    Mar 7 at 18:57






  • 1





    Are you sure that when you're calling Object.search, the parameter that you're passing is a string and not a number?

    – actaram
    Mar 7 at 19:01











  • Very dirty indeed.

    – Bergi
    Mar 7 at 19:05











  • @zynth666 - updated

    – dgo
    Mar 7 at 19:13















0















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. 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(). 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];. 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.










share|improve this question



















  • 1





    What does the rest of your code look like?

    – zynth666
    Mar 7 at 18:57






  • 1





    Please include examples of how you're calling Object.prototype.search

    – Gershom Maes
    Mar 7 at 18:57






  • 1





    Are you sure that when you're calling Object.search, the parameter that you're passing is a string and not a number?

    – actaram
    Mar 7 at 19:01











  • Very dirty indeed.

    – Bergi
    Mar 7 at 19:05











  • @zynth666 - updated

    – dgo
    Mar 7 at 19:13













0












0








0








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. 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(). 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];. 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.










share|improve this question
















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. 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(). 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];. 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.







javascript types






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 19:19







dgo

















asked Mar 7 at 18:53









dgodgo

2,4462033




2,4462033







  • 1





    What does the rest of your code look like?

    – zynth666
    Mar 7 at 18:57






  • 1





    Please include examples of how you're calling Object.prototype.search

    – Gershom Maes
    Mar 7 at 18:57






  • 1





    Are you sure that when you're calling Object.search, the parameter that you're passing is a string and not a number?

    – actaram
    Mar 7 at 19:01











  • Very dirty indeed.

    – Bergi
    Mar 7 at 19:05











  • @zynth666 - updated

    – dgo
    Mar 7 at 19:13












  • 1





    What does the rest of your code look like?

    – zynth666
    Mar 7 at 18:57






  • 1





    Please include examples of how you're calling Object.prototype.search

    – Gershom Maes
    Mar 7 at 18:57






  • 1





    Are you sure that when you're calling Object.search, the parameter that you're passing is a string and not a number?

    – actaram
    Mar 7 at 19:01











  • Very dirty indeed.

    – Bergi
    Mar 7 at 19:05











  • @zynth666 - updated

    – dgo
    Mar 7 at 19:13







1




1





What does the rest of your code look like?

– zynth666
Mar 7 at 18:57





What does the rest of your code look like?

– zynth666
Mar 7 at 18:57




1




1





Please include examples of how you're calling Object.prototype.search

– Gershom Maes
Mar 7 at 18:57





Please include examples of how you're calling Object.prototype.search

– Gershom Maes
Mar 7 at 18:57




1




1





Are you sure that when you're calling Object.search, the parameter that you're passing is a string and not a number?

– actaram
Mar 7 at 19:01





Are you sure that when you're calling Object.search, the parameter that you're passing is a string and not a number?

– actaram
Mar 7 at 19:01













Very dirty indeed.

– Bergi
Mar 7 at 19:05





Very dirty indeed.

– Bergi
Mar 7 at 19:05













@zynth666 - updated

– dgo
Mar 7 at 19:13





@zynth666 - updated

– dgo
Mar 7 at 19:13












1 Answer
1






active

oldest

votes


















1














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(/.*/));








share|improve this answer

























  • This is indeed cleaner

    – dgo
    Mar 8 at 19:10










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55050909%2fextending-js-prototype-argument-is-getting-type-coerced%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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(/.*/));








share|improve this answer

























  • This is indeed cleaner

    – dgo
    Mar 8 at 19:10















1














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(/.*/));








share|improve this answer

























  • This is indeed cleaner

    – dgo
    Mar 8 at 19:10













1












1








1







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(/.*/));








share|improve this answer















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(/.*/));








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(/.*/));





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(/.*/));






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 11 at 15:05

























answered Mar 7 at 20:15









Gershom MaesGershom Maes

1,9621330




1,9621330












  • This is indeed cleaner

    – dgo
    Mar 8 at 19:10

















  • This is indeed cleaner

    – dgo
    Mar 8 at 19:10
















This is indeed cleaner

– dgo
Mar 8 at 19:10





This is indeed cleaner

– dgo
Mar 8 at 19:10



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55050909%2fextending-js-prototype-argument-is-getting-type-coerced%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme