What's the purpose of Facebook's __d method? [duplicate] - javascript

This question already has answers here:
What is Facebook's function __d
(3 answers)
Closed 9 years ago.
I've noticed something interesting what Facebook do, and I'd like to know what they are doing and why? If you look at their source code on one of their .js files they seem to be doing a lot of this:
__d("AjaxRequest",["Erro...
__d("FBAjaxRequest",["AjaxRequest"...
__d("CallbackManagerController",...
There seems to be no variables or ordinary functions and objects in any of their JavaScript files. There are functions, but there all arguments passed to this __d method, like this:
__d("keyMirror",[],function(a,b,c,d,e,f){var g=function(h){var i={},j;if(!h)return h;for(j in h){if(!h.hasOwnProperty(j))continue;i[j]=j;}return i;};e.exports=g;});
Is this some sort of optimization for JavaScript, or is it used for easier management?
Thanks

Maybe just a wrapper that does things like dependency injection and error handling. If you can find out where __d is define, you'll know for sure.
It doesn't look like an optimizing (ie. a packer).

Related

Symbol behind the .js [duplicate]

This question already has answers here:
Why pass parameters to CSS and JavaScript link files like src="../cnt.js?ver=4.0"?
(9 answers)
Closed 3 months ago.
What is "?_=3.5-SNAPSHOT-Dev" mean after ".js"? I do some research on google but no idea what it mean and what it use for
<script type="text/javascript" src="js/jquery-3.5.1.js?_=3.5-SNAPSHOT-Dev"></script>
It depends on the server that's serving your Javascript. Generally, variables like those on JS files are used for cache busting. I think in your example, 3.5-SNAPSHOT-Dev could be a release tag. If this JS file is now on your own machine, you can safely discard the ? and anything after that. If you're getting this from another server somewhere, seek out documentation about that server to see what they use the _ variable for.

Colon after colon syntax while calling a function in Javascript [duplicate]

This question already has an answer here:
Colon after function declaration in javascript [duplicate]
(1 answer)
Closed 4 years ago.
I came across a syntax somewhere on the web recently and couldn't grasp its meaning.
What I understand is that when we write props: Object inside the brackets, it means we're assigning a default value to props as Object. But what does the 2nd colon signify? It looks like a key-value pair but is confusing me still.
Tried searching on the web but wasn't able to search due to lack of terminology. Any ideas what this means?
someFn(props: Object): Object {
return someOtherFn(props);
}
These are type annotations. They are not standard javascript. They are added when using tools that layer static typing onto javascript. The two most popular flavors are Typescript and Flow.
When you write code that uses this syntax you will transpile your source code into code that is syntactically valid for execution by running one of the above mentioned tools on your code. When you do, it will tell you if your usage of the types is correct, raise warnings that are helpful in development, and then strip all this out so it can actually be run.

Where can i find the code source of javascript functions? [duplicate]

This question already has answers here:
Where can I find javascript native functions source code? [duplicate]
(3 answers)
Closed 1 year ago.
For example, i want to know how the querySelector method was built. I checked on MDN but there's only examples that show how to use it.
I also tried to check in the console.log but nothing readable.
There is a non-standard method to do this. Please be aware that it is not fully cross-browser compatible and shouldn't be used in a production environment.
function hello(){
return "Hi!";
}
console.log(hello.toSource());
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/toSource for more.
Edit:
To see the source code of a built-in function, you will need to look at the engine source. This varies browser to browser. For example, Chrome uses the V8 engine. See https://github.com/v8/v8

How to avoid this javascript eval()? [duplicate]

This question already has answers here:
How to execute a JavaScript function when I have its name as a string
(36 answers)
Closed 7 years ago.
I'm trying to rid my code of a couple of evals that I have in my javascript, but I'm not sure of the best way to achieve what I'm trying to do.
I have a "filter form" on a bunch of my pages, with a bit of JS attached to it that reloads parts of the page depending on what the user does.
Different pages require different actions though.
So my solution (that I came up with yearrrrs ago...) was
<form name="callback_loadCalendar">
<form name="callback_loadNames">
Etc.
And this horrible bit of JS (attached to onchange events etc) to then call the relevant function:
if (f.getAttribute('name') && f.getAttribute('name').indexOf('callback') === 0)
eval(f.getAttribute('name').substr(9)+'()');
E.g. that would call loadCalendar() and loadNames() respectively.
What SHOULD I be doing instead?
Thanks!
If the functions are in the global scope, then you can use bracket notation in the global scope to access the function references.
window[f.getAttribute('name').substr(9)]();

How to protect javascript function? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How can I obfuscate JavaScript?
Is there any way to protect my js file or javascript function from user to view?
You could minify/obfuscate it but the function would still be visible to the user.
Nope but you can always obfuscate. just look up online for javascript obfuscator. It makes code harder to read but it'll still be decodable.
If you need to hide code may I suggets something serverside such as php, aspx etc..
Like Darin sayd, you can obfuscate the code.
But if you want execute a js file on a browser, the user can get the source code. There is no way to avoid this.

Categories

Resources