我有以下代码,但我觉得很脏..
我不喜欢写这么多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) 在调用实例时,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)