小编Rog*_*Rog的帖子

我该如何改进这段代码?我不喜欢这么多的ifs

我有以下代码,但我觉得很脏..

我不喜欢写这么多ifs然后在每个if中重复代码.

关于如何改进此代码的任何想法?

char obj[5];
strlcpy(obj, &jarr[i], arr[i]);
if( !strcmp( obj, "led_r" ) ){
    i++;
    strlcpy( obj, &jarr[i], arr[i] );
    red_brightness = atoi( obj );

    Serial.print(" RED: ");
    Serial.println( red_brightness );
}
if( !strcmp( obj, "led_g" ) ){
    i++;
    strlcpy( obj, &jarr[i], arr[i] );
    green_brightness = atoi( obj );

    Serial.print(" GREEN: ");
    Serial.println( green_brightness );
}
if( !strcmp( obj, "led_b" ) ){
    i++;
    strlcpy( obj, &jarr[i], arr[i] );
    blue_brightness = atoi( obj );

    Serial.print(" BLUE: ");
    Serial.println( blue_brightness );
}
Run Code Online (Sandbox Code Playgroud)

c arduino

2
推荐指数
1
解决办法
108
查看次数

类的默认方法

在调用实例时,JS有一种方法可以执行默认方法吗?

示例:假设我有以下类命名MyClass,然后我启动这个类的实例命名foo,我希望当我调用fooexecute default方法时MyClass.

class MyClass {
  constructor () {
    this.props = {
        question: 'live, universe, everything',
        answer: 42
    }
  }

  default () {
    return this.props
  }

  hello () {
    return 'hello world'
  }
}
const foo = new MyClass()

// execute the default method
console.log(foo) // log: {question: 'live, universe, everything', answer: 42}

// execute hello method
console.log(foo.hello()) // log: hello world
Run Code Online (Sandbox Code Playgroud)

javascript node.js ecmascript-6

1
推荐指数
1
解决办法
1797
查看次数

标签 统计

arduino ×1

c ×1

ecmascript-6 ×1

javascript ×1

node.js ×1