你能解释一下这个javascript代码吗? [英] Can you explain this following javascript code?

查看:116
本文介绍了你能解释一下这个javascript代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让calculatorIsOn = false;



const pressPowerButton =()=> {

if(calculatorIsOn){

console.log('计算器关闭。');

calculatorIsOn = false;

}其他{

console.log('计算器打开。');

calculatorIsOn = true;

}

};



pressPowerButton();

//输出:计算器开启。



pressPowerButton();

//输出:计算器关闭。



什么我试过了:



我想知道为什么他们在第四行中分配另一个值calculatorIson = false; ..还想了解整个程序..

请帮助我理解我这是我在JavaScript的早期阶段..

先谢谢你..

let calculatorIsOn = false;

const pressPowerButton = () => {
if (calculatorIsOn) {
console.log('Calculator turning off.');
calculatorIsOn = false;
} else {
console.log('Calculator turning on.');
calculatorIsOn = true;
}
};

pressPowerButton();
// Output: Calculator turning on.

pressPowerButton();
// Output: Calculator turning off.

What I have tried:

I wanted to know that why they assign another value in 4th line calculatorIson =false; .. and also want to understand whole program..
please help me understand me this I am at very early stage of JavaScript..
Thanks in Advance..

推荐答案

实际上它是 TypeScript [ ^ ]

Javascript中的等效代码是

Actually it is TypeScript [^]
The equivalent code in Javascript is
var calculatorIsOn = false;
      function pressPowerButton() {
          if (calculatorIsOn) {
              console.log('Calculator turning off.');
              calculatorIsOn = false;
          }
          else {
              console.log('Calculator turning on.');
              calculatorIsOn = true;
          }
      };
      pressPowerButton();
      // Output: Calculator turning on.
      pressPowerButton();
      // Output: Calculator turning off.





浏览这些您将了解的链接

让TypeScript中的关键字 [ ^ ]

const·TypeScript Deep Dive [ ^ ]

=>打字稿箭头函数 [ ^ ]



参考TS代码的内联评论



go through these links you will get to know
let keyword in TypeScript [^]
const · TypeScript Deep Dive[^]
=> Typescript Arrow Functions[^]

refer inline comments for TS code

  let calculatorIsOn = false;  // global variable declaration

        const pressPowerButton = () => {  // defining a function , refer arrow functions
            if (calculatorIsOn) {          // check whether the variable is true 
console.log('Calculator turning off.');
        calculatorIsOn = false;                 // set the variable  as false 
        } else {
            console.log('Calculator turning on.');  // log the result in the console window 
            calculatorIsOn = true;          // set the variable as true 
        }
        };
        pressPowerButton(); // call the function 


它使用calculatorIsOn作为全局变量来定义计算器是否打开,电源按钮用作切换,如果当电源被按下时,必须关闭电源,当电源关闭时它必须打开,因此代码只是切换calculatorIsOn的值。代码可简化为简单



It is using calculatorIsOn as a global variable to define if the calculator is on or not and the power button acts as a toggle so if the calc is on when the power is pressed it has to be off, and when it is off it has to be on, so the code is simply toggling the value of calculatorIsOn. The code could be simplified to simply

calculatorIsOn = !calculatorIsOn;





如果值为false,则值为true;如果为真,则为false。



That will make the value true if it is false and false if it is true.


这篇关于你能解释一下这个javascript代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆