这个conf有什么作用?

时间:2016-10-18 00:42:05

标签: actionscript-3

所以我在网上看了一个教程并且发现了这个:

 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];
}

有人可以帮助解释第2行和第8行的含义吗?感谢您帮助新的编码器!

2 个答案:

答案 0 :(得分:1)

我添加了一些评论并重命名了参数以使其更清晰:

//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];
}

答案 1 :(得分:1)

问题似乎特定于变量赋值中的theme_set(theme_bw()) ggplot(data = df[1:10,])+ geom_errorbar(aes(x = X, ymin = mean - se, ymax = mean + se))+ geom_point(aes(x = X, y = mean))+ geom_line(aes(x = X, y = mean), group = 1)+ geom_hline(data = df[11,], aes(yintercept = mean, colour = 'A'))+ geom_hline(data = df[12,], aes(yintercept = mean, colour = 'B')) 构造。正如@Thilo所提到的,如果参数中缺少该字段,它只是一种指定默认值的方法。

例如:

||

如果在调用函数function read_file(file, delete_after) { delete_after = delete_after || "False"; //rest of code } 时未传递变量delete_after,那么它将假定值read_file"False"符号之后的任何内容。

有些人更喜欢对||进行明确检查。

其他指示:

相关问题