好!欢迎访问迷津渡口 现在时间是: 天气 °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
   支持一下
挤眼 亲亲 咆哮 开心 想想 可怜 糗大了 委屈 哈哈 小声点 右哼哼 左哼哼 疑问 坏笑 赚钱啦 悲伤 耍酷 勾引 厉害 握手 耶 嘻嘻 害羞 鼓掌 馋嘴 抓狂 抱抱 围观 威武 给力
 点赞
 签到
 表情
 图片
 代码
提交评论

清空信息
关闭评论
warrnamboolag
 warrnamboolag 约 12 小时前回复
#161
<a href="http://www.profancyshop.com/air-jordan-retro-14-hommesthe-verte-rose-chaussuresk">air jordan retro 14 hommesthe verte rose</a> <a href="http://www.lmcgroep.com/the-north-face-jester-jacket-2017-northfaceh">the north face jester jacket 2017</a> <a href="http://www.nowaitnails.com/nike-air-presto-gr%C3%BCn-blau-schuhel">nike air presto gr眉n blau</a> <a href="http://www.xmrujo.com/nike-free-hyperfeel-run-bleu-chaussuresq">nike free hyperfeel run bleu</a> <a href="http://www.sevenguard.com/hombres-nike-zoom-pegasus-33-p%C3%BArpura-azul-zapatosl">hombres nike zoom pegasus 33 p煤rpura azul</a> <a href="http://www.escortsue.com/kvinders-nike-air-max-90-vt-lilla-gr%C3%A5-skoj">kvinders nike air max 90 vt lilla gr氓</a>
warrnamboolag http://www.warrnamboolag.com/
ptsbarwinslow
 ptsbarwinslow 2019-04-23 13:35回复
#160
<a href="http://www.bgaddiict.com/nike-free-run-5.0-kvinner-sapphire-bl%C3%A5-obuvi">nike free run 5.0 kvinner sapphire bl氓</a> <a href="http://www.deriogolf.com/nike-air-force-1-high-gym-rosso-scarpek">nike air force 1 high gym rosso</a> <a href="http://www.poluscompany.com/air-jordan-3-white-shoesi">air jordan 3 white</a> <a href="http://www.pariswebjob.com/nike-blazer-low-verde-zapatosi">nike blazer low verde</a> <a href="http://www.dakvideo.com/reebok-jetfuse-light-gul-sort-skoh">reebok jetfuse light gul sort</a> <a href="http://www.freshkeema.com/nike-roshe-nero-run-scarpei">nike roshe nero run</a>
ptsbarwinslow http://www.ptsbarwinslow.com/
noir violet converse
 noir violet converse 2019-04-23 09:52回复
#159
<a href="http://www.muebleactual.com/auburn-tigers-top-of-the-world-ncaa-society-adjustable-cap-hath">auburn tigers top of the world ncaa society adjustable cap</a> <a href="http://www.xfullfilmizle.com/adidas-ultra-boost-noir-uk-chaussuresh">adidas ultra boost noir uk</a> <a href="http://www.mycryptoverse.com/adidas-prougeator-lz-indoor-chaussuresj">adidas prougeator lz indoor</a> <a href="http://www.blokistand.com/alle-rot-nike-air-presto-schuhek">alle rot nike air presto</a> <a href="http://www.wadeesley.com/oakland-raiders-15-michael-crabtree-olive-2017-salute-to-service-limited-jersey-nflh">oakland raiders 15 michael crabtree olive 2017 salute to service limited jersey</a> <a href="http://www.dermonenc.com/menn-adidas-springblade-solyce-svart-r%C3%B8d-obuvi">menn adidas springblade solyce svart r酶d</a>
noir violet converse http://www.viaggivaldata.com/noir-violet-converse-chaussuresl
dominiogold
 dominiogold 2019-04-22 07:00回复
#158
<a href="http://www.sumumcatering.com/north-face-denali-womens-xs-northfaceh">north face denali womens xs</a> <a href="http://www.amsinconline.com/nike-hypershift-rot-wei%C3%9F-schuhek">nike hypershift rot wei脽</a> <a href="http://www.cavestt.com/nike-air-zoom-pegasus-31-m%C3%A6nd-hvid-lime-gr%C3%B8n-skoi">nike air zoom pegasus 31 m忙nd hvid lime gr酶n</a> <a href="http://www.deshiresult.com/noir-and-vert-mizuno-volleybtout-chaussures-chaussuresh">noir and vert mizuno volleybtout chaussures</a> <a href="http://www.quentsons.com/virginia-beach-hat-hatl">virginia beach hat</a> <a href="http://www.steugenesband.com/air-jordan-retro-10-blanc-violet-bleu-chaussuresh">air jordan retro 10 blanc violet bleu</a>
[url=http://www.dominiogold.com/]dominiogold[/url]
goficarwash
 goficarwash 2019-04-22 04:49回复
#157
<a href="http://www.reviewtogel.com/r%C3%B8d-hvid-herre-air-jordan-jumpman-team-sko-skoh">r酶d hvid herre air jordan jumpman team sko</a> <a href="http://www.igrejamormon.com/adidas-boost-blu-arancia-scarpei">adidas boost blu arancia</a> <a href="http://www.skonya.com/maschio-mizuno-wave-prophecy-2-blu-arancia-scarpej">maschio mizuno wave prophecy 2 blu arancia</a> <a href="http://www.monsterplate.com/houston-rockets-white-jersey-nflh">houston rockets white jersey</a> <a href="http://www.kentimpendik.com/buy-coach-wristlet-online-coachh">buy coach wristlet online</a> <a href="http://www.attivopizza.com/nike-roshe-maschio-wool-skin-nero-rose-rosso-scarpei">nike roshe maschio wool skin nero rose rosso</a>
goficarwash http://www.goficarwash.com/
allstudentsshuttle
 allstudentsshuttle 2019-04-21 19:46回复
#156
<a href="http://www.aworldtranslation.net/nike-air-force-1-mid-07-all-svart-kurpesf">nike air force 1 mid 07 all svart</a> <a href="http://www.backyardescapes.net/new-blance-620-bambini-oro-rosa-schuhed">new blance 620 bambini oro rosa</a> <a href="http://www.bairdauctions.net/flyknit-air-max-lyser%C3%B8d-orange-zest-obuvg">flyknit air max lyser酶d orange zest</a> <a href="http://www.batmadrid.net/nike-roshe-run-hyperfuse-gris-hombres-kurpesf">nike roshe run hyperfuse gris hombres</a> <a href="http://www.beckerny.net/nike-free-rosa-r%C3%B8d-kurpesf">nike free rosa r酶d</a> <a href="http://www.beckyobrien.net/kyrie-3-gelb-icon-schuheg">kyrie 3 gelb icon</a>
allstudentsshuttle http://www.allstudentsshuttle.net/
seusiteimobiliario
 seusiteimobiliario 2019-04-21 06:55回复
#155
<a href="http://www.txmed.net/adidas-protator-hg-kunstrasen-schuhec">adidas protator hg kunstrasen</a> <a href="http://www.ukpubs.net/nike-free-haven-3.0-seahawks-for-vendita-butyg">nike free haven 3.0 seahawks for vendita</a> <a href="http://www.uniquewebsitedesign.net/jordan-retro-10-ice-vert-scarpec">jordan retro 10 ice vert</a> <a href="http://www.unratedperez.net/adidas-prougeator-instinct-solar-bleu-neon-rose-chaussuresf">adidas prougeator instinct solar bleu neon rose</a> <a href="http://www.urbanintelligence.net/mennns-air-yeezy-ii-2-sp-max-90-rosa-svart-butya">mennns air yeezy ii 2 sp max 90 rosa svart</a> <a href="http://www.utsweb.net/air-jordan-8-s%C3%B8lv-gr%C3%B8n-schuhea">air jordan 8 s酶lv gr酶n</a>
seusiteimobiliario http://www.seusiteimobiliario.net/
onlysidekick
 onlysidekick 2019-04-21 05:10回复
#154
<a href="http://www.romoautocare.com/nike-air-force-1-mid-vert-abyss-chaussuresh">nike air force 1 mid vert abyss</a> <a href="http://www.industrialegy.com/hombres-asics-asics-gel-kayano-17-negro-zapatosp">hombres asics asics gel kayano 17 negro</a> <a href="http://www.bewustvitaal.com/nike-zoom-pegasus-34-womens-sky-blue-purple-shoesi">nike zoom pegasus 34 womens sky blue purple</a> <a href="http://www.flushandhunt.com/herren-nike-air-zoom-structure-18-wei%C3%9F-schuhei">herren nike air zoom structure 18 wei脽</a> <a href="http://www.kendalfewster.com/nike-free-og-14-gris-zapatosk">nike free og 14 gris</a> <a href="http://www.gisusalon.com/kvinders-nike-flyknit-agility-r%C3%B8d-skoi">kvinders nike flyknit agility r酶d</a>
onlysidekick http://www.onlysidekick.com/
dermonenc
 dermonenc 2019-04-19 21:46回复
#153
<a href="http://www.xgaajgeg.com/adidas-samba-super-blanc-chaussuresh">adidas samba super blanc</a> <a href="http://www.bisnistrading.com/nike-air-max-thea-violet-chaussuresj">nike air max thea violet</a> <a href="http://www.axxisdrilling.com/nike-huarache-ultra-premium-se-ingrotients-schuhek">nike huarache ultra premium se ingrotients</a> <a href="http://www.bunqdesktop.com/von-miller-youth-jersey-nflh">von miller youth jersey</a> <a href="http://www.iamsubscribed.com/adidas-ultra-boost-rosa-bl%C3%A5-obuvi">adidas ultra boost rosa bl氓</a> <a href="http://www.bcstormwater.com/nike-lunarepic-low-flyknit-white-shoesj">nike lunarepic low flyknit white</a>
dermonenc http://www.dermonenc.com/
dissertationsverlag
 dissertationsverlag 2019-04-19 09:52回复
#152
<a href="http://www.dietecon.net/nike-kobe-9-alle-gul-kurpesd">nike kobe 9 alle gul</a> <a href="http://www.digilowcost.net/jordan-retro-1-blanco-mettodasic-plata-schuhed">jordan retro 1 blanco mettodasic plata</a> <a href="http://www.digitalgoldrush.net/nike-lunarglide-7-verde-grigio-kurpesf">nike lunarglide 7 verde grigio</a> <a href="http://www.dimaf.net/nike-lebron-12-femminile-arancia-argento-obuva">nike lebron 12 femminile arancia argento</a> <a href="http://www.dionamago.net/nike-sb-stefan-janoski-max-wolf-grau-mesh-schuhe-dam%C3%A4nner-obuvf">nike sb stefan janoski max wolf grau mesh schuhe dam盲nner</a> <a href="http://www.disorderedliberty.net/nike-air-force-1-high-premium-schwarz-crinkle-patent-obuvf">nike air force 1 high premium schwarz crinkle patent</a>
dissertationsverlag http://www.dissertationsverlag.net/
顶部留言底部
 00:00/00:00
我想和你虚度时光 - 花房姑娘(9)
  1. 我想和你虚度时光
  2. 花房姑娘
  3. 恋恋风尘
  4. 我喜爱一切不彻底的事物
  5. 我的心里是满的
  6. Long Way
  7. 给少年的歌
  8. 晴日共剪窗
  9. 天上的月你的脸