Pedro Ferreira
Pedro Ferreira

Reputation: 493

The meaning of colon (:), can be... what? Similary to equal(=)?

Excluding ":this()" and within "if()" statements, what can possible mean the ":" operator?

In the next example, sort of irrelevant, I want to find, within a collection of <a href> links, either of 2 things:

So, for instance, on this code:

init_stuff = function(objToWhom_Id) { 
   $ibItems        = $ibWrapper.find('div.ib-main > a');
   $ibImgItems     = $ibItems.not('.ib-content');

   if(objToWhom_Id == "allHTMLitems"){
       imgItemsCount = $ibImgItems.length;
   }else{
       ++imgItemsCount;
       //Here we are; notice the ":"
       $ibImgItems : $ibItems.find('span:first').filter(':contains(objToWhom_Id)');
       //
       console.log($ibImgItems.html();
   }
}

everything works fine (the 'init_stuff()' function is Deferred, btw)...

But if we change the above line to:

       $ibImgItems = $ibItems.find('span:first').filter(':contains(objToWhom_Id)');

which should be the reasonable thing, doesn't work anymore... :-P

Any help?


EDITED

To test ":" works, you can add (as actually the main thing has!) an event to the pointed object, inside init_stuff() right at the end:

$ibImgItems.bind('click.ibTemplate', function(clik_$ibImgItem) {
    clik_$ibImgItem.preventDefault();
    openItem($(this));
    return false;
});

It works, @Hans! ;-)

Upvotes: 4

Views: 208

Answers (3)

Pedro Ferreira
Pedro Ferreira

Reputation: 493

Although not so "clean" as the one provided by @Hans, here is another way of avoiding "label", using another 'outer' "filter":

var $ibImgItems = $ibItems
                     .filter(function(){
                          return $(this).find('span:first').filter(':contains('+objToWhom_Id +')');
                     });

I think it goes better with the logical of "label", in order to replace it, in this very particulary case. Just a thought.

Thanks again for all contributions!

Upvotes: 0

Howard Renollet
Howard Renollet

Reputation: 4739

The colon in JavaScript can be used as a ternary operator

TestExpression ? ValueIfTrue : ValueIfFalse

Or it could be used as a label

var i = 100, a = 100;
outerloop:
while(i > 0) {
  while(a > 0) {
   a++
   if(a>50) {
     break outerloop;
   }
  }
  i++
}

Or it can be used to assign key/value pairs

var MyObject = { keyName1: "value1", keyName2: "value2" }

Upvotes: 2

Hans Roerdinkholder
Hans Roerdinkholder

Reputation: 3000

The :-operator should not do anything here, except make sure that the line fails. I have a feeling that your line doesn't work as is, and starts working when you replace the : with an =-operator.

There seems to be another mistake:

$ibImgItems = $ibItems.find('span:first').filter(':contains(objToWhom_Id)');

Change this to:

$ibImgItems = $ibItems.find('span:first').filter(':contains(' + objToWhom_Id + ')');

Does that by any chance solve the problem?

Edit: to answer your question a bit more: one case where : equals = is in object literal syntax:

var object = { property: value, someOtherProperty: someOtherValue };

Upvotes: 2

Related Questions