|
So, I figured it couldn't hurt for use to share our tricks between coders. I'll start with a quick easy one - forcing variables to be numbers in type. There's two easy ways, subtract 0 or multiply by 1. This forces the type to be an integer/float/number. Example: var notANumber = "2"; alert(typeof notANumber); // string var aNumber = notANumber-0; alert(typeof aNumber); // number
This trick allows you to force math usage where it sometimes gets tricky since a number is a string. It's also a way around using parseInt to force a numerical value on a string. So, let's hear some of yours. I'll continue to post mine.
|
|
|
|
|
I don't have any .... I just code the problem when and if I see it. I never thought of doing what you have done.
Last Edit: Feb 23, 2008 13:09:02 GMT by Michael
|
|
|
|
Heh. There's lot of little tricks out there like this. Another one (more of a function) is finding the position of an element for dynamic positioning, etc. function position(ext) { var o = { left: ext.offsetLeft, top: ext.offsetTop }; while(ext.offsetParent){ o.left += ext.offsetLeft; o.top += ext.offsetTop; ext = ext.offsetParent; } return o; } I picked this trick up from Cali about 2 years ago... he originally got it from QuirksMode, and I must say it's a god-send if you ever need to find the position of an element. Just call the function on the element you want the position of, and it'll return an object with the properties of "left" and "top." I think you can guess what they contain.
|
|
|
|
|
for(var x = 6, a = 1; x > 0; x --, a ++) (a % 2 ? x - 1 : x + 1) // Returns numbers in order 5, 6, 3, 4, 1, 2
Easy way to return the index of an element of an array, though not recommended if you've got multiple instances of the same data (["hey", "hey"]).
var i = [ ["lol", "rofl"], ["lmao", "omfg"] ] i[1][i[0].join(",").split("lol")[0].split(",").length - 1] // returns "lmao"
I've used this countless times, particularly in my image rollovers for PB
Last Edit: Feb 24, 2008 8:05:40 GMT by Aaron
|
|
|
|
Aaron: I still prefer other methods for doing your version of the array keys. Anyways, here's my next trick. Apparently Aaron didn't know it, so I figure it's worth mentioning. It's called "Negative look ahead" in RegExp. Hechizero explained it quite well: In other words, what it does is it looks ahead. Then, if it finds the text inside the parenthesis, it breaks. It's called "negative look ahead" because it looks ahead and wants to NOT find the content. A "positive look ahead" looks ahead and WANTS to find the content. One example of an use in a location check: !location.href.match(/action=(?!home)/) // Main page
|
|
|
|
|
Hmmmm Normal RegExp will do me!
|
|
|
|
Hmmmm Normal RegExp will do me! It managed to confuse Aaron at first, so that's acceptable.
|
|
|
|
|
I'd read about that, but never could figure out the syntax for JS.
|
#intj (Mastermind)^
|
|
|
|
It's more or less the same (and just as helpful.) Anyways, here's my next one. Beta recently PMed me about this, so I figured I'd mention it. "Using || (or) to get data" var mightBeTrue = Math.round(Math.random()); // This is either randomly true or false (0 or 1). var safetyValue = 2; // This is the "safety value" to drop back to incase the above is false
var checkHolder = mightBeTrue || safetyValue; // Assigns mightBeTrue to checkHolder if it's true and assigns safetyValue if mightBeTrue is false.
alert(["mBT: "+mightBeTrue, "sV: "+safetyValue, "cH: "+checkHolder].join("\n")); I'm less sure if this one will help anyone, but it's worth a mention.
Last Edit: Mar 5, 2008 2:53:10 GMT by Chris
|
|
|
|
|
I'm fairly certain it's not Math.rand() Anyway, that's less of a trick, and more just proper understanding of the operator. A lot of people see at as returning true or false, rather than returning one of its two operands based on the boolean value of the first. In which case, here's my last contribution "8" - -4 z0mg
|
|
|
|
Some contribution. Noob. But yeah, typo there. (I didn't actually test the code... It's all conceptual.) And yeah, I understand it's just understanding how it works. Most people don't though. That's why I mentioned it.
|
|
|
|
|
Nah, I was following your lead.
|
|
|