-4

So I was looking at a tutorial online and came across this:

 function generateRobot(conf:Object = null):Robot {
            var conf:Object = conf || {};
            var defaults:Object = {
                laserColor:red,
                personality: "evil"
            }
for (var key:String in defaults){
    conf[key] = conf[key] || defaults[key];
}

Can someone help explain what line 2 and line 8 mean? Thanks for helping a new coder!

fiat.lucy
  • 1
  • 3
  • In this code, the `conf` does not do anything. But it is being built from passed in values with some defaults for missing fields. This should be configuration for the Robot to be generated here. Presumably some code later on is reading from `conf`. – Thilo Oct 18 '16 at 00:44
  • 3
    Is there some sort of library/framework being using in this example? I don't think JavaScript has a `variable:type` syntax in general. – Spencer Wieczorek Oct 18 '16 at 00:47
  • I think this might be Unity3d – JanR Oct 18 '16 at 00:49
  • This looks like AS3 to me. – zzzzBov Oct 18 '16 at 00:52
  • https://unity3d.com/learn/tutorials/topics/scripting/c-vs-js-syntax – JanR Oct 18 '16 at 00:52
  • I found this here: https://code.tutsplus.com/tutorials/whats-a-configuration-object-and-why-bother-using-it--active-11580 and I thought that it was javascript but looking at it closer I see that it's action script. I guess this is why I was down voted, sorry about that! I thought that conf was just a term in javascript I haven't heard of, not a defined variable. Thanks for the help everybody! – fiat.lucy Oct 18 '16 at 02:24

2 Answers2

1

I have added some comments and renamed the param to make it clearer:

//param is a parameter of the type object with a default value of null that is passed 
//into the function, if nothing is passed in it will be null
function generateRobot(param:Object = null):Robot { 
            //declare a local variable called conf and populate 
            //it with the  parameter if it exists, otherwise create a new object {}
            var conf:Object = param || {};
            //create a default settings object
            var defaults:Object = {
                laserColor:red,
                personality: "evil"
            }
//loop through the default settings
for (var key:String in defaults){
    //conf setting becomes param if exists otherwise use the defaults value
    conf[key] = conf[key] || defaults[key];
}
JanR
  • 6,052
  • 3
  • 23
  • 30
1

The questions seems specific to the || construct in the variable assignment. As @Thilo mentioned, it is simply a way to specify a default, should the field be missing in the parameter.

For example:

function read_file(file, delete_after) {
    delete_after = delete_after || "False";
    //rest of code
}

would be such that, if variable delete_after is not passed when function read_file is called, then it will assume value "False", or anything after the || sign.

Some prefer an explicit check against undefined.

Other pointers to look at:

Community
  • 1
  • 1
sal
  • 3,515
  • 1
  • 10
  • 21