Converting a function to its string representation [closed] - javascript

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I know that calling toString on function object would give me the string representation of the function definition like:
function foo() { /*...*/ }.toString(); works fine for me.
But here
function stringifyFn(fn) {
// Support: Chrome 50-51 only
// Creating a new string by adding `' '` at the end, to hack around some bug in Chrome v50/51
// (See https://github.com/angular/angular.js/issues/14487.)
// TODO (gkalpak): Remove workaround when Chrome v52 is released
return Function.prototype.toString.call(fn) + ' ';
}
I see that author has used this pattern in many places, what could be the reasons behind it?

Related

How to set variable of image src and use in javascript function? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
The community is reviewing whether to reopen this question as of 2 years ago.
Improve this question
So if i have some codes like this
If(sceneA){document.getElementById("divA").src="A.png";}
If(document.getElementById("divC")=="A.png"){document.getElementById("divB").src="B.png";}//And alot of codes here
How can I use a var to shorten strings like
document.getElementById("divA").src="A.png"
document.getElementById("divB").src="B.png"
so i can use them both inside (IF) and {function}
As they have to repeat quite a few times in the codes.
Thanks alot!
That's exactly what functions are for.
Define a function for repeating blocks of code as follows:
function setSrc(divName, imgName){
document.getElementById("div"+divName).src = imgName+".png";
}
function srcEquals(divName, imgName){
return (document.getElementById("div"+divName).src == imgName+".png");
}
Then you can replace your code with this:
if(sceneA){setSrc("A","A");}
if(srcEquals("C", "A")){setSrc("B","B");}

How to show method declaration in NodeJs [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
is there any way to show function declaration. e.g what it takes as parameters and what it returns in CLI? as like "tinker in laravel"
I searched over internet but found nothing.
I am not sure if I correctly understand your question. To find out what a function looks like, including its parameters (and all its code including what it returns), you can do a (slightly dirty) hack like this:
function a(b,c) {
return b + ", " + c;
}
console.log(a.toString());
Output:
'function b(a,b){return a+" " + b}'
Alternatively, if you are looking for documentation, for most NPM modules and note itself, there is solid API documentation. See https://nodejs.org/api/ for example.

Which is preferred for typecasting to a String in JavaScript? String() or template literal? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Let's say I have some variable that is probably a number.
Is it preferred to use String() or use a template literal to convert this variable to a string?
const n = 3.14;
let strN = String(n);
// OR
strN = `${n}`;
or does it not make any difference other than the obvious readability issues?
airbnb's styleguide suggests String(n).
// => this.reviewScore = 9;
// good
const totalScore = String(this.reviewScore);
airbnb Coercion: strings
You Don't Know JS - Types & Grammar: Coercion

Replace javascript on Google Chrome [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an event:
javascript: AbrePesquisa ('/page/Test.asp?parm1=False');
The AbrePesquisa
function:
AbrePesquisa function (url)
{
url = url.replace = ("?" , "$$$");
...
}
In the IE, the url is coming with "$$$" ('/page/Test.asp$$$parm1=False') but it comes in chrome with "$$" ('/page/Test.asp$$parm1=False').
Why is this happening?
If you look at the docs for String.prototype.replace you'll see that $ is a special character used to include parts of the matched string in the replacement. To get what you want, you'll have to do url.replace("?", "$$$$$$");

Validation on special codes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a special ID in the format of A000000004..A000000150 etc and OD000000001..OD000000150. I have read the forums here and tried out the solutions (many in the form of /^([A]{1}\[0-9]{9})$/ etc.) but none of them have helped.
I hope I'm understanding your question properly, but how about this regex?
/^(A|OD)[0-9]{9}$/
To check a string against this regex, you'd use something like this:
var regex = /^(A|OD)[0-9]{9}$/;
if (regex.test(myCode)) {
// ... do something ...
}
***So the final solution to my question for people who will refer in future:
The code will be:
var regex= /^(A|OD)[0-9]{9}$/;
var myCode=document.forms["formname"]["fieldname"].value;
if(!regex.test(myCode))
{
alert(...);
return false;
}

Categories

Resources