What does this code mean?

Hey can someone please tell me what this code mean?

function envFlush(a){function b(c){for(var d in

Looks like some of your example is missing.

Yeah you have right, but can you tell me what it means if you give you all the code?

Sorry I mean if I give you all my code, can you then tell me what the line of code I don’t understand mean, if I tell you what lines I don’t understand?

I (or someone else here) can at least give you a better idea of what you’re looking at. Keep the code sample small and you should get a good answer.

But how can I get the code small?

You have the definition of a function and in this definition you have the definition of another function, which starts with a for loop.

If it’s more than a few dozen lines of code, you don’t want to post it here. You can post a link to the code, then copy just the bits that you have questions about.

It’s part of the first function in the script tags on Facebook.

Someone already suggested to you that trying to understand the Facebook code was going to be difficult because it is minified, which means it is practically unreadable for humans.

If you want to learn to make something like Facebook, don’t look at their production code, look at their features and learn to build them your self.

Essentially it’s a frontend that includes forms and dynamic content (jQuery to be easy, React to use more modern methods), and a backend that has a database you read from and write to.

Don’t skip the actual learning - work through the curriculum at https://freecodecamp.org and eventually you’ll be able to build what you want.

Okay, here is the link:
view-source:https://www.facebook.com/
The link is from view to .com/
I don’t understand line 3.

Wait, you want us to explain how Facebook works? Uhh…

But briefly,

function envFlush(a) {

is the beginning of a function declaration. I assume you’ve done some of those.

Next is another function declared within that,

function b(c) {

Then you have a for loop

for ( var d in a)
     c[d]=a[d]

This is a for/in loop that loops through the properties in the object a.

What does it do? That would mean analyzing large portions of the Facebook code. I would hazard to say that you aren’t ready for that. It would be like some one in premed asking to do brain surgery.

Part of the meaning of the code is obfuscated because they have minimized the javascript - removed unnecessary spaces and new lines to allow it to load faster.

That entire JS block would be this, unminified:

function envFlush(a) {
    function b(c) {
        for (var d in a) 
            c[d] = a[d]
    }
    if (window.requireLazy) 
        window.requireLazy(["Env"], b);
    else {
        window.Env = window.Env || {};
        b(window.Env)
    }
}
envFlush({
    "ajaxpipe_token": "AXjY4vUuqRgIYbL6"
});

What does it do? I don’t know, I’d have to spend hours of code trying to understand it. You’d be better served by focusing on basic JS topics and build up those so you’ll be ready to do things like this later.

1 Like

I don’t know what I’m supposed to do with this information, but @ksjazzguitar was able to post the relevant code.

function envFlush(a) {
    function b(c) {
        for (var d in a) 
            c[d] = a[d]
    }
    if (window.requireLazy) 
        window.requireLazy(["Env"], b);
    else {
        window.Env = window.Env || {};
        b(window.Env)
    }
}
envFlush({
    "ajaxpipe_token": "AXjY4vUuqRgIYbL6"
});

To understand this function, go to Facebook.com, open up the console and type window.Env. You’ll see an object:

{ajaxpipe_token:"AXjUbc4buKkVw2BR",
dom_mutation_flag:true,
khsh:"0`sj`e`rm`s-0fdu^gshdoer-0gc^eurf-3gc^eurf;1;enbtldou;fduDmdldourCxO`ld-2YLMIuuqSdptdru;qsnunuxqd;rdoe-0unjdojnx-0unjdojnx0-0gdubi^rdbsduOdv-0`sj`e`r-0q`xm`r-0StoRbs`qhof-0mhoj^q`xm`r",
nocatch:undefined,
sample_continuation_stacktraces:true,
shouldLogCounters:true,
start:1510548928168,
timesliceBufferSize:5000,
timeslice_categories:{react_render: true, reflow: true},
timeslice_heartbeat_config:{pollIntervalMs: 33, idleGapThresholdMs: 60, ignoredTimesliceNames: {…}, enableOnRequire: false},
__proto__:Object
}

I don’t know what any of these values are used for, but the name gives me a clue. Env (short for ‘environment’) variables are used for run time configuration. So, each of these is somehow used to configure the way Facebook works in your browser. The function you are interested in takes a configuration object and applies it to the window.Env object. Basically, the envFlush function is how Facebook devs configure the app.

Minification is lossy. Pretty printing it makes it a little easier to follow, but you’re still missing all the original variable names and comments (along with some other stuff, though most of it doesn’t matter too much).

Agree with this. Or if you’re interested to know how those features might be implemented in real production code, look up open-source social networks instead; you’ll be able to view their actual, unminified source code on Github or a similar platform.

Can someone please explain me what all the Facebook code means, except line 1 and 2.

As Randell says, you should focus on your basic code understanding.

In order to understand what that code is doing, we’d have to see all of the code, preferably the source code and spend hours going through it. Asking us to explain that code would be like handing you a piece of some random machine and asking you what it does. What does the machine do? What section is it part of? Where are the schematics?

This code snippet is relying on variables and functions declared in other parts of the code. You are asking us to explain one of the most sophisticated software systems on the planet, based on a tiny snippet of code.

Again, focus on your fundamentals. Don’t show up on the first day of flight school and demand to do a carrier landing.

1 Like

Closed as no longer productive.