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?
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
|
show 1 more comment
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
1
What does the rest of your code look like?
– zynth666
Mar 7 at 18:57
1
Please include examples of how you're callingObject.prototype.search
– Gershom Maes
Mar 7 at 18:57
1
Are you sure that when you're callingObject.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
|
show 1 more comment
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
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
javascript types
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 callingObject.prototype.search
– Gershom Maes
Mar 7 at 18:57
1
Are you sure that when you're callingObject.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
|
show 1 more comment
1
What does the rest of your code look like?
– zynth666
Mar 7 at 18:57
1
Please include examples of how you're callingObject.prototype.search
– Gershom Maes
Mar 7 at 18:57
1
Are you sure that when you're callingObject.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
|
show 1 more comment
1 Answer
1
active
oldest
votes
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(/.*/));
This is indeed cleaner
– dgo
Mar 8 at 19:10
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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(/.*/));
This is indeed cleaner
– dgo
Mar 8 at 19:10
add a comment |
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(/.*/));
This is indeed cleaner
– dgo
Mar 8 at 19:10
add a comment |
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(/.*/));
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(/.*/));
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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