Home > Web Front-end > JS Tutorial > body text

JS uses switch to determine the code sharing of ternary operation while and attribute operation

巴扎黑
Release: 2017-09-04 10:10:04
Original
1558 people have browsed it

This article mainly introduces JS switch to judge the ternary operation while and attribute operation code. Friends who need it can refer to

Ternary operation: such as var a = 10;


var b= 12;
c = a>b ?a:b;
Copy after login

If true, execute a, otherwise execute b


var isHide = true;
Copy after login

If you use if to judge the statement as follows


if(isHide) {
box.style.display = "block";
} else {
box.style.display = "none";
}
Copy after login

Ternary operation replaces if


box.style.display = isHide?"block":"none";
Copy after login

Judgment condition? The execution statement is established: the execution statement is not established.

If the condition is true, return: the result of the execution is true

The condition is not true: the result of the execution

Simplified version of if judgment

continue Skip this loop: cannot write ternary operation

column:


for(var i=0;i<5;i++){
if(i==3)
continue;跳过i=3这个循环不执行;其他依然执行。
}
Copy after login

break: terminate the entire loop; by default, only the immediately adjacent loop will be terminated If you want to terminate other loops, you need to give the loop a name.

Example:


for(var i=0;i<5;i++){
if(i==3)
break;当循环执行到i=3时终止整个循环。
}
name:for(var i=0;i<5;i++){
for(var j=0;j<5;j++){
break name //终止两个循环
}
}
Copy after login

The object stores data, which is stored in the properties of the object:

The object is a complex type of data , we can store any type of data in the object

The first type: attribute operation 1 The first type of attribute operation is a set of hard-coded values, and variables cannot be used.

Read operation obj.Attribute name

Write operation obj.Attribute name = attribute value;

obj.key = val;Key value pair;

For example,


var obj = {};
obj.name = "Leo"; 这种写法为第一种属性名都被写死若想调用必须要如
obj.age = 40; 下console.log(obj.name);。
obj.gender = "female";
console.log(obj.w);//注意第一种属性操作是写死的一个值,不能使用变量 obj.w 这么写就认为在找 obj的w属性。
Copy after login

The second attribute operation is done. What is received is a string. The string can be stored in a variable. You can directly write obj[variable Call]

Read operation obj["Attribute name]

obj["Attribute name"]=Attribute value;


obj["key"] = val;
Copy after login

Note The second type

such as


var obj = {};
obj[&#39;name&#39;] = "Leo";
obj[&#39;age&#39;] = 40;
obj[&#39;gender&#39;] = "female";
obj[&#39;width&#39;] = "180px";
obj[&#39;height&#39;] = "180px";
obj[&#39;children&#39;] = ["大明","小明","三明"];
var w = "width";
console.log(obj[w]);// 第二种属性操作,接受的是一个字符串,字符串就可以存在变量中,可以直接写obj[变量调用];
alert("name" in obj); // obj 有 name属性就返回 true,否则 返回false
Copy after login

determines whether there is this attribute in the object

returns true or false


for (var s in obj) { 找到obj的所有属性 s会依次变成arr的属性名 //同过in方法将循环变量s变成obj对
console.log(s,obj[s]); 应的属性名。若想使用只有用第二中命名。s的类型是字符串。
} s为属性名 obj[S]为对应的属性名的属性值。
Copy after login

for in can be used to loop objects and arrays but cannot be used to loop a group of elements.

for loop can only be used to loop arrays (a group of elements) and cannot be used Loop through a group of objects


##

###while语句###
起始值
while(判断条件){
执行语句;
自增;
}
##switch##
switch(要判断的内容){
case:判断内容; //条件成立后执行语句将case下所有代码都执行,只有遇见break才会结束
执行语句; 若没有遇见break就会遇见穿刺现象执行所有代码。
break;
default: //以上所有的判断都不成立
}
Copy after login

Example

##

switch(a){
case 10://当a == 10;
console.logo(“;;;;”)语句结束之后添加break
break;//当a==10 的
case 15://当a==15;
console.logo("ppppp")
break;
default: //以上所有的判断都不成立
}
Copy after login

The above is the detailed content of JS uses switch to determine the code sharing of ternary operation while and attribute operation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!