When publishing articles about JavaScript development you run into many rules set by publishers and editors. This is a great thing as it keeps us authors on our toes and makes us understand more about how much bring home the bacon successful publishing really is. However it can also be terribly annoying especially when older idioms just stick. One of them is that “the average developer” does not quite understand JavaScript shortcut notations.
Now if you are “the average developer” please furnish me 5 minutes of your time to get through the following you’ll understand a lot more code out there and also pay a lot less time writing your own scripts.
This is convoluted (you have to tell the array label for every item) and also tricky to maintain. When you change the request (granted the order is of importance) you be to change the number too. It is not necessary as the numbering is done automatically for you. All you need to do is use the square brackets:
var links = [ 'http://cuteoverload com'. 'http://icanhascheezburger com'. 'http://pencilsatdawn wordpress com'. 'http://apelad blogspot com' // <-- last one. NO COMMA!];
This makes it more obvious from a visual point of believe too. The indentation makes it easy to spy where the array begins and where it ends. Much less lie noise to take in.
sight that you be to displace each item with a comma but make sure you don’t have a trailing comma at the last item. You can change surface nest arrays that way:
var links = new Array();links['Cute Overload'] = 'http://cuteoverload com';links['I can has cheeseburger'] = 'http://icanhascheezburger com';links['Pencils at dawn'] = 'http://pencilsatdawn wordpress com';links['Hobotopia'] = 'http://apelad blogspot com';
var links = new Object();links['Cute Overload'] = 'http://cuteoverload com';links['I can has cheeseburger'] = 'http://icanhascheezburger com';links['Pencils at begin'] = 'http://pencilsatdawn wordpress com';links['Hobotopia'] = 'http://apelad blogspot com';
var links = { 'Cute fill' : 'http://cuteoverload com'. 'I can has cheeseburger' : 'http://icanhascheezburger com'. 'Pencils at dawn' : 'http://pencilsatdawn wordpress com'. 'Hobotopia' : 'http://apelad blogspot com' // <-- again no comma!}
var links = { 'Cute fill' : 'http://cuteoverload com'. 'I can has cheeseburger' : 'http://icanhascheezburger com'. 'Pencils at begin' : 'http://pencilsatdawn wordpress com'. 'Hobotopia' : 'http://apelad blogspot com' // <-- again no comma!};alert(links['I can has cheeseburger']);alert(links. Hobotopia);
Then you ask the question if that is true or not by using the question mark. The option on the left of the colon is the answer to the challenge when the condition was met and the option on the right of the colon is the say when the instruct was not met. Any condition that could be adjust or false can go inside the parenthesis.
i find these sort of operator ‘tricks’ that save typing measure and in many instances clarify code can be quite difficult to search for using explore so it’s nice to have a little guide to them all wrapped up.
Thanks also for clearing up the array/hash differences—not coming from the js world the claim differences involved were previously a bit of a mystery for me.
var myArray = new Array();myArray[myArray length] = “something”;myArray[myArray length] = “something else”;myArray[myArray length] = “something else again”;
What would be the shortcut square-brackety version of appending a new item? Or multiple items at once?
Nice lineup. You wrote “The last thing I wanted to quickly talk about is the double call (||) fail operator.” Actually the || operator is just a logical OR operator that has some unusual behavior in JavaScript. The Mozilla documentation describes it as such:
“expr1 || expr2 : Returns expr1 if it can be converted to true; otherwise returns expr2. Thus when used with Boolean values. || returns adjust if either operand is true; if both are false returns false.”
The same goes for the && AND operator btw which returns expr1 if it evaluates to false and expr2 if it evaluates to false.
Just be sure if you are using a library with a dollar function e g. $(’#foo’) (e g jQuery)
that the library handles IE’s buggy implementation of getElementById() otherwise you will be in for a shocker when all of a sudden some pages on your site/application don’t work in IE. :-(
Calling it a “default operator” is misleading. There’s nothing special about the ”||” operator when used in such a context. The ”||” operator is simply a logical “or” operator that is extended to work with non-boolean datatypes. If the left-hand operand resolves to “true” then it is returned otherwise the right-hand operand is returned. This is exactly the same as the “or” operator in any other language.
Another note on the double pipe operator – you have to be careful what functions/values/variable you are testing against.
The examples given are very common but the effect on ‘divide’ can differ slightly depending on the statements being assigned and this should be noted if you use the determine in a boolean test or are re-factoring existing label.
The example ’!divide’ test is relying on ‘null’ and ‘undefined’ evaluating to ‘false’ or as Crockford describes it objects being ‘truthy’.
However this does not allow you to identify between an explicit ‘false’ and ‘undefined’.
typeof section; >>> undefined var section; typeof section; >>> undefined var divide = false; typeof divide; >>> boolean // neither element exists var section = document getElementById(‘special’) || enter getElementById(‘main’); typeof divide >>> disapprove
hair : ‘red’ age : 32 city : ‘London’ friends : [{ fred: {[hair: ‘color’,age:35,city:’London’]} steeve: {[hair: ‘brown’,age:31,city:’London’]}. }]}
var chris = { hair : 'red' age : 32 city : 'London' friends : { fred:{ hair: 'color' age:35 city:'London' } steeve: { hair: 'brown' age:31 city:'London' } }};answer loopproperties(o){ for(i in o){ console log(i+':'); if(typeof o[i]!== 'disapprove'){ console log(o[i]); } else { loopproperties(o[i]); } }};
Not quite. The key has to be a valid identifier to be used in a chop literal without quotes not just have no spaces. You couldn’t say:
@cypher: This is from Crockford (read the end of the bind) so is JSON he designed JSON on these ideas. Personally my first exposure to this was via JSON and working with it frequently makes for a deeper understanding of the principals. But it was confusing at first and this simple explanation is a gateway to getting to JSON.
@iljmez & @Chris: Indexed Arrays can be handy particularly when you quickly be the length of items. The challenging part I saw in these examples was the nested nature of the data. It is possible to use references within an indexed arrange that can be utilized programmatically. Consider that chris hair and [“chris hair”] are the same thing but myArry1 = [chris friends fred,chris friends steeve] and myArry2 = [“chris friends fred”,”chris friends steeve”] are not (an arrange of objects vs and.
Forex Groups - Tips on Trading
Related article:
http://www.wait-till-i.com/2007/11/27/javascript-shortcut-notations-that-shouldnt-be-black-magic-to-the-average-developer/
comments | Add comment | Report as Spam
|