javascript - Coffeescript ternary if-statement wrong logic -



javascript - Coffeescript ternary if-statement wrong logic -

i have pretty much simple logic in homecoming function, doesn't work expected. of course of study can create code longer , solve issue, want little possible.

here code:

#return title if exists or false otherwise getpagetitlefrommaincontent = (maincontent) -> maincontent.find('#pagetitle') ?.length ?= false if y = (getpagetitlefrommaincontent $("#maincontent")) y.css color:red

as see, if finds #pagetitle in #maincontent, should create red. function doesn't homecoming #pagetitle if found, returns .length.

from js2coffee.org see code compiled into:

var getpagetitlefrommaincontent, y; getpagetitlefrommaincontent = function(maincontent) { var _ref, _ref1; homecoming (_ref = maincontent.find('#pagetitle')) != null ? (_ref1 = _ref.length) != null ? **_ref1 : _ref.length = false : void 0;** }; if (y = getpagetitlefrommaincontent($("#maincontent"))) { y.css({ color: reddish }); }

and should _ref : _ref.length = false : void 0;, not _ref**1** : _ref.length = false : void 0; .

http://jsfiddle.net/x8vjj/1/

thank you!

not sure code makes sense. you're trying assign length property, unless length defined. if defined, returns length property. looks code , behaviour correct, understanding of existential operator , homecoming values wrong. if want homecoming found element need disconnect length check.

maybe like:

getpagetitlefrommaincontent = (maincontent) -> arr = maincontent.find('#pagetitle') if arr.length arr else false

as ian explained in more elegant answer, not need utilize existential operator on arr (assuming jquery), since array of elements (with 0 length if not found).

javascript jquery if-statement coffeescript

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -