简体   繁体   English

这些摘要之间有什么区别?

[英]What is the difference between these snippets?

New to javascript, I face many problems. javascript新手,我遇到许多问题。

I read the javascript tutorial at w3cschools.com, and there are many question marks on my head. 我在w3cschools.com上阅读了javascript教程,并且头上有很多问号。

I do not understand what is the difference below: 我不明白下面有什么区别:

var name=something;

name=something;

The above two examples also giving something to a name, why 2 different ways? 上面的两个示例也给名称起了什么作用,为什么有两种不同的方式?

name=new Array();

name[0]=something0;

name[1]=something1;

is this same with switch? 开关也一样吗?

//switch start //切换开始

var name=something();

switch(something)

{

case 1:

do something;

break;

case 2:

do something;

break;

default:

do something;

}

//if...else start //如果...否则开始

var name=something();

if (condition) 

{

do something

};

else if (condition)

{

do something

};

else

{

do something

};

what is the different between switch case and else.if ? 开关盒和else.if有什么区别?

i think both 2 is doing the same thing?match condition and then do something? 我认为两个2都在做同一件事?匹配条件然后做点什么?

and the for Loops,while Loops and break Loops , 和for循环,而Loop和Break循环,

both 3 are doing the same thing,but 3 different ways. 两者都在做同一件事,但是三种方式不同。

can someone tell me what is the different between them?it make me confuse. 有人能告诉我他们之间有什么区别吗?这让我感到困惑。

and please intro more tutorial for javascript. 并请介绍更多有关javascript的教程。

many thanks here 非常感谢这里

For your first question, when u use "var", it defines a local scope to the variable. 对于第一个问题,当您使用“ var”时,它将为变量定义局部作用域。 When you use variables without keywork "var", it means they are global variable. 当您使用没有键值“ var”的变量时,表示它们是全局变量。 Usually its not a good practice to use global variables. 通常,使用全局变量不是一个好习惯。

Also on other IF ELSE parts, you cant have semicolon before ELSE IF block. 同样在其他IF ELSE零件上,您不能在ELSE IF块之前加分号。

Check these links - 检查这些链接-

Read all articles under JavaScript: http://www.crockford.com/ 阅读JavaScript下的所有文章: http : //www.crockford.com/

JavaScript: Scoping and Hoisting: http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting JavaScript:作用域和提升: http//www.adequatelygoodgood.com/2010/2/JavaScript-Scoping-and-Hoisting

JavaScript: Function expressions vs. Function declarations vs. Function statements: http://yura.thinkweb2.com/named-function-expressions/ JavaScript:函数表达式与函数声明与函数语句:http: //yura.thinkweb2.com/named-function-expressions/

In answer to your first question: 在回答您的第一个问题时:

var name=something;

name=something;

var name 变量名

This snippet creates a new variable, called name. 此代码段创建一个新的变量,称为name。 It will be refered to in the rest of your code as name, it has been declared. 它已经在您的代码的其余部分中称为名称。

var name = something var name =某物

This piece of code assumes that there is a variable declared above it called 'something', and it creates the name variable and assigns it the value of whatever 'soemething' holds at that particular point. 这段代码假定在其上方声明了一个名为“ something”的变量,并创建了名称变量,并为该变量分配了该特定点上所有“ soemething”所具有的值。

name = something 名称=某物

Without the intitial creation of the variable, this line assumes that the variable has already been declared previously, it is simply assigning that variable the value or something. 如果没有初始创建变量,则此行假定该变量先前已经声明过,它只是为该变量分配值或其他内容。

Reading

I recommend you read this webpage which appears to have a great introduction to javascript and will answer many more of your questions. 我建议您阅读此网页,该网页似乎对javascript进行了很好的介绍 ,并将回答您的许多其他问题。

and a quick, easy answer to your switch question. 并快速,轻松地回答您的切换问题。 switch is just often cleaner and more readable than a repeated else if block. 开关通常比重复的if块更干净,更易读。
You can also do cool stuff like regular expressions in your case (case /awesome|radical/: ...). 您还可以在您的案例中做一些很酷的事情,例如正则表达式(案例/ awesome | radical /:...)。 You will appreciate that later ;) 稍后您会感激的;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM