好!欢迎访问迷津渡口 现在时间是: 天气 °C ~ °C

迷津渡口

越感到迷惑 越接近真理

javascript中逻辑运算符和逗号运算符的流程控制功能

逻辑运算符&&的短路特性:

&&运算过程:

首先计算左边表达式的值:
  若值为假或相当于假的值,则直接返回左边表达式的值,不继续计算右边表达式;
  若值为真或相当于真的值,则继续计算右边表达式的值,然后返回右边表达式的值。

例子:

// 左边为假的情况,直接返回左边表达式的值
var a = false && 10;
alert(a);			// false
a = 1<0 && 10;
alert(a);			// false
a = null && 10;
alert(a);			// null
a = undefined && 10;
alert(a);			// undefined
a = NaN && 10;
alert(a);                       // NaN
a = 0 && 10;
alert(a);			// 0
a = '' && 10;
alert(a);			// ''
a = 0.0 && 10;			// 0.0....后面可接多个0
alert(a);			// 0

// 左边为真的情况,继续右边表达式的计算,返回右边表达式的值10
a = true && 10;
alert(a);
a = 1>0 && 10;
alert(a);
a = '0' && 10;
alert(a);
a = {} && 10;
alert(a);
a = [] && 10;
alert(a);
a = function() {} && 10;
alert(a);
a = function() { return 0; } && 10;
alert(a);

与&&运算符流程控制表达式

可以直接丢弃表达式的值,直接采用&&的短路特性进行流程控制,一个小例子:

function a() {
	return false;
}
function b() {
    return true;
}
function c() {
    alert(d);
}
var d = '第一个表达式c函数不被调用,没有输出';
a() && c();
d = '第二个表达式c函数被调用,输出';
b() && c();

逻辑运算符或||的短路特性:

||运算过程:

首先计算左边表达式的值:
  若值为真或相当于真的值,则直接返回左边表达式的值,不继续计算右边表达式;
  若值为假或相当于假的值,则继续计算右边表达式的值,然后返回右边表达式的值。

例子:

// 左边为真的情况,返回左边表达式的值
var a = true || 10;
alert(a);                            // true
a = 1>0 || 10;
alert(a);                            // true
a = '0' || 10;
alert(a);                            // '0'
a = {} || 10;
alert(a);                            // [object Object]
a = [] || 10;
alert(a);                            // ''
a = function() {} || 10;
alert(a);                            // function () {}
a = function() { return 0; } || 10;
alert(a);                            // function () {return 0;}

// 左边为假的情况,继续右边表达式的计算,返回右边表达式的值10
a = false || 10;
alert(a);
a = 1<0 || 10;
alert(a);
a = null || 10;
alert(a);
a = undefined || 10;
alert(a);
a = NaN || 10;
alert(a);
a = 0 || 10;
alert(a);
a = '' || 10;
alert(a);
a = 0.0 || 10;	    // 0.0....后面可接多个0
alert(a);

或||运算符流程控制表达式

小例子:

function a() {
	return false;
}
function b() {
    return true;
}
function c() {
    alert(d);
}
var d = '第一个表达式c函数被调用,输出';
a() || c();
d = '第二个表达式c函数不被调用,没有输出';
b() || c();

逗号运算符

逗号运算符运算过程:

逗号运算符表示从左到右计算表达式的值,返回最后一个表达式的值。
需要注意:逗号运算符的优先级是最低的。

例子:

var a;
// 逗号,运算符优先级低于赋值运算符=,因此a为1
a=1,2,3;
alert(a);
// 利用括号改变优先级,此时a为3
a=(1,2,3);
alert(a);

逗号运算符流程控制表达式:

分号确定表达式结束,区别例子:var连续声明

// 逗号运算符实现连续声明
alert(a);alert(b);alert(c);    // 均输出undefined,表示都声明成功
var a=1,b=2,c=3;
alert(''+a+b+c);               // 输出'123'

// 分号造成错误
alert(a);    // a声明成功,输出undefined
alert(b);    // b声明失败,报错,停止向下运行
alert(c);
var a=1;b=2;c=3;
alert(''+a+b+c);

利用逗号表达式获取函数返回值:

function a() {
    var b = 1;
    var c = 2;
    return b++,c++,b+c;
}
// 首先b++,则b=2,然后c++,则c=3,最后b+c=5,则完整逗号表达式的值为5,函数返回5
alert(a());    // 输出5


小指才疏学浅,有任何疏漏之处,请不吝赐教~

点赞0
   支持一下
挤眼 亲亲 咆哮 开心 想想 可怜 糗大了 委屈 哈哈 小声点 右哼哼 左哼哼 疑问 坏笑 赚钱啦 悲伤 耍酷 勾引 厉害 握手 耶 嘻嘻 害羞 鼓掌 馋嘴 抓狂 抱抱 围观 威武 给力
 点赞
 签到
 表情
 图片
 代码
提交评论

清空信息
关闭评论
giyimaskilari
 giyimaskilari 约 8 小时前回复
#258
<a href="http://www.abiliator.com/adidas-eqt-bambini-scarpe-marrone-blu-chaussuresb">adidas eqt bambini scarpe marrone blu</a> <a href="http://www.tapsevilla.com/nike-air-max-tailwind-7-hombres-corriendo-zapato-company-zapatosj">nike air max tailwind 7 hombres corriendo zapato company</a> <a href="http://www.sanduicheria.com/adidas-crazylight-boost-guld-s%C3%B8lv-obuvf">adidas crazylight boost guld s酶lv</a> <a href="http://www.clavideo.com/pandora-bracelets-canada-bread-pandoraj">pandora bracelets canada bread</a> <a href="http://www.usnbraces.com/nike-free-run-3.0-v5-hvid-trainersd">nike free run 3.0 v5 hvid</a> <a href="http://www.kinmountfair.com/nike-air-max-95-junior-nero-converse-chaussuresm">nike air max 95 junior nero converse</a>
giyimaskilari http://www.giyimaskilari.com/
syunpatsu
 syunpatsu 2019-12-14 18:49回复
#257
<a href="http://www.charleyscafe.com/nike-air-max-bw-schwarz-rosa-kurpesb">nike air max bw schwarz rosa</a> <a href="http://www.emmuscades.com/ralph-lauren-stripe-polo-verde-marr%C3%B3n-polos">ralph lauren stripe polo verde marr贸n</a> <a href="http://www.esmokecanada.com/mackage-belted-down-coat-mackager">mackage belted down coat</a> <a href="http://www.steamfestnz.com/nike-tiempo-legend-5-sortout-chaussuresf">nike tiempo legend 5 sortout</a> <a href="http://www.toockbespoke.com/air-max-black-design-shoese">air max black design</a> <a href="http://www.vindianescort.com/ugg-5825-classic-short-boots-black-dresses-uggsa">ugg 5825 classic short boots black dresses</a>
[url=http://www.syunpatsu.com/]syunpatsu[/url]
tustinvillage
 tustinvillage 2019-12-14 11:48回复
#256
<a href="http://www.charleyscafe.com/air-max-1-lunar-infrarouge-grill-chaussuresb">air max 1 lunar infrarouge grill</a> <a href="http://www.emmuscades.com/ugg-bailey-button-37-uk-uggse">ugg bailey button 37 uk</a> <a href="http://www.esmokecanada.com/nike-foamposite-kvinders-lyser%C3%B8d-shoesd">nike foamposite kvinders lyser酶d</a> <a href="http://www.steamfestnz.com/nike-flyknit-lunar-1-m%C3%A4nner-rosa-chaussuresd">nike flyknit lunar 1 m盲nner rosa</a> <a href="http://www.toockbespoke.com/pandora-ring-for-engagement-pandorar">pandora ring for engagement</a> <a href="http://www.vindianescort.com/herre-salomo-s-lab-xt-himmelbl%C3%A5-bl%C3%A5-kurpese">herre salomo s lab xt himmelbl氓 bl氓</a>
tustinvillage http://www.tustinvillage.com/
texomatruss
 texomatruss 2019-12-14 07:49回复
#255
<a href="http://www.munchpartners.com/nike-zoom-winflo-3-herren-laufen-schuhe-jordan-chaussuresb">nike zoom winflo 3 herren laufen schuhe jordan</a> <a href="http://www.mdieight.com/air-max-1-triple-bianca-gum-sole-zapatosj">air max 1 triple bianca gum sole</a> <a href="http://www.graindescenes.com/nike-air-max-2016-violet-nails-obuvf">nike air max 2016 violet nails</a> <a href="http://www.achataltkirch.com/pandora-disney-charms-hk-key-pandoraj">pandora disney charms hk key</a> <a href="http://www.brianpeiris.com/air-jordan-13-bl%C3%A5-gr%C3%A5-hvid-trainersd">air jordan 13 bl氓 gr氓 hvid</a> <a href="http://www.photolacasa.com/air-max-95-salg-jd-chaussuresm">air max 95 salg jd</a>
texomatruss http://www.texomatruss.com/
peloepeso
 peloepeso 2019-12-13 15:56回复
#254
<a href="http://www.ampmduischool.com/pandora-winter-charm-set-pandorad">pandora winter charm set</a> <a href="http://www.chonmon.com/mujeres-nike-free-flyknit-chukka-p%C3%BArpura-trainersi">mujeres nike free flyknit chukka p煤rpura</a> <a href="http://www.rostovobuv.com/nike-cortez-marine-blau-uk-chaussuresg">nike cortez marine blau uk</a> <a href="http://www.bouldersteps.com/ralph-lauren-sweater-mens-stripe-sweater-poloa">ralph lauren sweater mens stripe sweater</a> <a href="http://www.peloepeso.com/nike-free-nero-viola-scarpee">nike free nero viola</a> <a href="http://www.cambriapets.com/bilder-av-hvite-blomsterpike-kjoler-dresse">bilder av hvite blomsterpike kjoler</a>
peloepeso http://www.peloepeso.com/
chonmon
 chonmon 2019-12-13 02:04回复
#253
<a href="http://www.getfape.com/femminile-asics-gel-noosa-tri-10-rosso-oro-zapatosd">femminile asics gel noosa tri 10 rosso oro</a> <a href="http://www.aimauctions.com/air-jordan-18-wei%C3%9F-schwarz-obuvl">air jordan 18 wei脽 schwarz</a> <a href="http://www.wishsupporter.com/pandora-earrings-nickel-menu-pandorad">pandora earrings nickel menu</a> <a href="http://www.revisionsbacs.com/nike-mercurial-vapor-berry-lyser%C3%B8d-for-salg-trainersi">nike mercurial vapor berry lyser酶d for salg</a> <a href="http://www.duvetboutique.com/nike-zoom-structure-18-mujeres-cielo-azul-negro-chaussuresg">nike zoom structure 18 mujeres cielo azul negro</a> <a href="http://www.bestmotosport.com/ralph-lauren-pants-r%C3%B8d-marinen-poloa">ralph lauren pants r酶d marinen</a>
chonmon http://www.chonmon.com/
yuriboguinia
 yuriboguinia 2019-12-12 05:27回复
#252
<a href="http://www.blackpointcpk.com/nike-internationalist-yellow-blue-quilts-sportsa">nike internationalist yellow blue quilts</a> <a href="http://www.tntkibris.com/ugg-australia-women-s-classic-mini-leather-studs-wallet-uggsb">ugg australia women s classic mini leather studs wallet</a> <a href="http://www.duponttesoro.com/air-jordan-retro-11-himmelbl%C3%A5-butyg">air jordan retro 11 himmelbl氓</a> <a href="http://www.cameroncurran.com/nike-cortez-svart-university-r%C3%B8d-butya">nike cortez svart university r酶d</a> <a href="http://www.cafedegino.com/mackage-coat-nyc-prices-mackagea">mackage coat nyc prices</a> <a href="http://www.fotoversorger.com/womens-nike-epic-react-flyknit-blue-canada-shoeso">womens nike epic react flyknit blue canada</a>
yuriboguinia http://www.yuriboguinia.com/
greatratesrus
 greatratesrus 2019-12-10 11:05回复
#251
<a href="http://www.thepondineer.com/hermes-double-sens-tote-festival-hermesc">hermes double sens tote festival</a> <a href="http://www.dancebaytown.com/nike-shox-avenue-marron-violet-footm">nike shox avenue marron violet</a> <a href="http://www.newsbloggy.com/parajumpers-denali-mens-aux-parajumpersa">parajumpers denali mens aux</a> <a href="http://www.magianuar.com/moncler-mid-length-puffer-coat-nails-monclera">moncler mid length puffer coat nails</a> <a href="http://www.carystyle.com/nike-air-max-orange-silber-chaussuresb">nike air max orange silber</a> <a href="http://www.ehrcontrols.com/dam%C3%A4nner-nike-air-rejuven8-mule-alle-rot-zapatosj">dam盲nner nike air rejuven8 mule alle rot</a>
greatratesrus http://www.greatratesrus.com/
raleighstyle
 raleighstyle 2019-12-10 06:31回复
#250
<a href="http://www.whatiownapp.com/herren-asics-gel-sendai-2-wei%C3%9F-rosa-trainersd">herren asics gel sendai 2 wei脽 rosa</a> <a href="http://www.veronafabric.com/nike-flyknit-air-vapormax-lila-silber-chaussuresm">nike flyknit air vapormax lila silber</a> <a href="http://www.votejohndavis.com/ralph-lauren-short-sleeve-poplin-shirt-nz-poloa">ralph lauren short sleeve poplin shirt nz</a> <a href="http://www.stillbaymotel.com/roshe-run-floral-space-bleu-scarpek">roshe run floral space bleu</a> <a href="http://www.ecosphereasi.com/floral-dresses-dubai-dresse">floral dresses dubai</a> <a href="http://www.dacsud.com/nike-zoom-clear-out-1-bianca-marrone-footi">nike zoom clear out 1 bianca marrone</a>
raleighstyle http://www.raleighstyle.com/
judigeitzen
 judigeitzen 2019-12-09 17:15回复
#249
<a href="http://www.opticapegaso.com/parajumpers-vest-undergarments-lady-parajumpersa">parajumpers vest undergarments lady</a> <a href="http://www.eletrobrasal.com/moncler-coat-black-mouth-monclera">moncler coat black mouth</a> <a href="http://www.lexingtonccdw.com/air-jordan-4-gr%C3%A5-gull-chaussuresb">air jordan 4 gr氓 gull</a> <a href="http://www.maudehairva.com/nike-air-max-tailwind-8-for-verkauf-ohio-zapatosj">nike air max tailwind 8 for verkauf ohio</a> <a href="http://www.hpcmonthly.com/yeezy-boost-350-v2-hvid-website-obuvf">yeezy boost 350 v2 hvid website</a> <a href="http://www.alkonadom.com/pandora-charms-wedding-xl-pandoraj">pandora charms wedding xl</a>
judigeitzen http://www.judigeitzen.com/
顶部留言底部
 00:00/00:00
我想和你虚度时光 - 花房姑娘(9)
  1. 我想和你虚度时光
  2. 花房姑娘
  3. 恋恋风尘
  4. 我喜爱一切不彻底的事物
  5. 我的心里是满的
  6. Long Way
  7. 给少年的歌
  8. 晴日共剪窗
  9. 天上的月你的脸