« first day (107 days earlier)      last day (4851 days later) » 

7:14 PM
Okay, is it funny to star everything I say?
My very first codes looked something like this:
if (x == 1 || x == 3 || x == 5 … || x == 101) {
    …
} else if (x == 0 || x == 2 || x == 4 || x == 6 … || x == 100) {
    …
}
And no, it's not just an odd-even comparsion, I've just simplified the example.
Now I've learned that switch() is much better (and nicer).
switch (x) {
    case 1: case 3: case 5: … case 101:
        …
        break;
    case 0: case 2: case 4: … case 100:
        …
        break;
}
 
@Nyuszika7H only because the things you say are silly
 
@Raynos So do you think this is silly?
1 hour ago, by Nyuszika7H
@coure06 You can use screen.availHeight, though it's better to use window.innerHeight. But you should make it 5 or 10 pixels smaller anyway, to avoid unneccessary scrollbars. Though it's better to use
 
@Nyuszika7H are you taking the piss? I mean seriously?
 
@Raynos Huh?
 
@Nyuszika7H that switch block. I can't tell whether your serious or making a joke. I mean you could actaully be just that bad at programming.
 
7:19 PM
@Raynos That's just one of my very first codes, after the if-elseif. Sorry, I didn't explain it.
 
You do know about (x % 2 === 0) right?
 
even?
(x % 2) === 0, since some languages mess with operator precedence (especially with bitwise)...
 
@ircmaxell yes. if you want odd (x % 2 === 1)
See here that % is lower then ===
 
fair enough
 
Yes I know about it. Quoting myself:
 
7:25 PM
I hate most implementations of % tho, since they are wrong
 
8 mins ago, by Nyuszika7H
And no, it's not just an odd-even comparsion, I've just simplified the example.
 
% is not an integer operation. It's a real number operation.
 
@Nyuszika7H still such code makes me rage.
 
yet most will fail if you put in 3.5 % 0.5
 
@ircmaxell % is the integer remainder operator in my eyes.
 
7:26 PM
@Raynos: it's remainder
it doesn't matter the relationship between a, b and r (for a mod b = r)
it hold for all of R
3 % 1.5 === 4.5 % 1.5 !== 4 % 1.5
@Nyuszika7H: Long chains of switch fall-throughs or large if statements are normally a sign of code-rot
if you have a number of cases to check for, use arrays and check bounds...
 
if (x % 2) {
    // odd
} else {
    // even
}
 
@IvoWetzel oi!
 
:kitten rchern
 
7:42 PM
@IvoWetzel I think underscore.js is awesome. What more do you need?
 
@Raynos See this:
 
@Nyuszika7H probably don't want to.
 
jQuery == awesome
underscore.js == awesome

jQuery + underscore.js === awesome
 
@IvoWetzel besides I'm not getting payed to evangolise the JS garden either :)
 
@Raynos Actually, I meant jQuery + underscore.js is a great combination. :P
 
7:44 PM
@Nyuszika7H Ew I don't want any jQuery in my underscore. Ewwww!
 
@Raynos That's you. :)
 
@Nyuszika7H besides, i'll just use backbone.js or underscore.js and that's just spam
 
@Raynos what's backbone.js? I haven't heard of it
 
yay more flawed micro benchs!
 
7:46 PM
bitwise and should perform better than modulo to figure if a number is odd or not
 
@Nyuszika7H google is your friend. Lightweight mvc couple with underscore + optional jQuery
 
(btw)
 
@jAndy stop that. Micro optimising DEVIL
 
@jAndy modulo is 35% slower. btw you don't need to use a for loop on jsPerf
 
hehe
 
7:47 PM
more like 92%
 
@jAndy I'm going to skew your code by putting in breakpoints and breaking the timer
bitwise is 9% slower
 
@Raynos: now you know my worst nightmares
 
Firefox Beta 4.0b11pre 9,412,189 / 682,019
 
@Raynos: maybe be, but only on FF
 
@jAndy you try micro optimising in LISP :p
 
7:47 PM
FF is wierdo
 
Im using chrome
 
> Testing in Chrome 10.0.648.6 on Windows Server 2008 R2 / 7
LOL
 
oh.. :p
also strange, Safari(mac) performs like 30% better
 
chrome 8.
 
LOL, jsPerf thinks Win7 x64 is Windows Server 2008 R2…
 
7:48 PM
both webkits
 
...#
v8 vs nitro
webkit is the rendering engine :P
 
bitwise is 14% slower in
 
jsperf explodes in chrome trunk
well
 
bitwise
=======
97,961,210
±43.86%
fastest

modulo
======
3,651,731
±0.58%
96% slower
 
7:49 PM
java explodes
 
Wow. modulu is 75% slower without the for loop & if check
 
@Raynos 96% for me.
 
takes up 99% CPU and 200 MB for a simple class that returns the current system time -.-
 
safari 87% faster (bitwise and)
 
Try running Chrome with the --enable-benchmarking flag. :)
 
7:50 PM
@jAndy -.- i & 1 vs 1 % 2 do your test properly. Of couse 1 % 2 is going to be jitted to 1
 
i liked that one
bitwise dominated
mod one is interesting
 
@Raynos: well I tried that link from Nyuszika7H
 
@jAndy your original test didn't check for the same thing. read it again
 
yay, messed it up. anyway & 1 should have an advantage in most browesrs
I guess safari is extreme with around 90%, but IE's should also perform much faster with it
 
yeah definitely
 
7:53 PM
@jAndy bit vs module is about 150% speed improvement
 
@Raynos: chrome8 seems to perform a modulo slightly faster (1-5%) but in general it's the better idea to use the bitwise and
but anyway, this really is a microoptimazation you mentioned it already
 
opera its barely slower
12%
really wish jsperf showed the percentages of the overall results
and color coded them
like it does the immediate results
 
It seems ++i is the fastest. Actually, ++i and i++ are almost the same speed.
 
stop
Don't worry about micro-optimizations like that
it doesn't matter at all
 
ircmaxell: well I disagree with that, at least I wouldn't say "at all"
 
7:59 PM
well for a normal web app I 100% agree, but if you are writing a game engine where methods are being called hundreds of times per second id say they reall ymatter
mostly the bitwise operators, for distance lookups, etc
 
in most cases it doesnt matter, but there are cases where its necessary to be as fast as possible
 
@jAndy: Nope, that's not true
There are cases where specific sections of code themselves need to be as fast as possible
 
and then again, if you KNOW that a particular pattern is faster than another one and its a minimum effort to do it, why wouldn't you do it ?
 
but there's never a case where all of the code needs to be fast. And it's not worth the readability and maintainability tradeoff if it's not necessary
and the only way to know if it's necessary is to test
 
Just build something that functions first and if you start running into performance problems then optimize later. For 98% of apps out there even garbage code runs pretty damn fast.
 
8:01 PM
And the only way to test, is to have a working system
So never optimize before you have a working system. Period
 
Well I always try to write my ecmascript as fast as possible, even at cost of readabilty. It's slowish after all
even if engines gets faster and faster, there are still lots of bottlenecks
 
Even if it's just a case of ++i is faster than i++. They are used in different contexts, and hence have different semantic meaning. So don't corrupt your sementic meaning by making a brute force subsitution that "one is always faster"
 
so then all for loops should be for(var i=0; i<myArray.length; i++)
 
Yes
 
No
 
8:03 PM
i consider a ref to the length a micro optimization
 
no?
 
but imo it makes a difference
 
for(var i=0, len=myArray.length; i < len; i++)
CACHE the length
 
yeah @jAndy that was the point i was making
 
or its accessed in every iteration
 
8:03 PM
thats what i always do
but i mean if you dont do that, it still runs fine
 
@jAndy: it's a micro-optimization to do that. You can do that, it's not "bad", but your code block proves WHY not to do it
 
this are little tiny things, and really micro
but at the end of the day
 
Run that code you just posted and tell me what happens?
 
it adds up
 
I agree its most important to have a working product, as fast as possible, but if you know the tricks along the way you should use them. How often do you get to go back through all your code and fix, or change things you didnt do as good as you should of to begin with.
 
8:05 PM
That is exactly why you shouldn't micro-optimize. By caching the length inline, you just killed the readability of that line. So much, that at first glance it looks fine. But you introduced a major bug that would only be detected by thinking about it (the antithesis of readable code) -- Note, the original code was for (var i = 0, len=myArray.length;i++)
 
@ircmaxell: whats the bug ?
 
uh... caching the length is not a micro optimization
unless you're only shipping to the latest and greatest
 
@jAndy: edit histories are a wonderful thing...
 
@ircmaxell: na lol, come one, I just typed it in too fast
 
@jAndy: that's the point though
 
8:07 PM
thats not the point lol
 
unless it was pointed it out, most people would miss the bug
 
as js devs we see this all the time though, thats perfectly readable to me.
 
could happen at any piece of code
anyways, if we're speaking about micro optimizing loops, we all should use reversed while loops anway :)
 
who mistypes a for loop?
 
@jAndy Well, some code looks more wrong. Such as for (var i = 0; i++)... But when you're expecting to see an myArray.length inside, and you do, your brain adds in the rest (whether or not it's there)
 
8:07 PM
for<tab> fills in the template
 
And that's the point. It's easy to optimize correct code. It's very difficult to correct optimized code...
Not to mention readability, etc
bbiaf
 
well true, you should know what youre doing
 
if you screw up a for loop... and you don't have unit tests... don't write JS code :P
 
but that is another story, I guess no other language beside ecmascript, has so many programers with so little knowledge about their language
 
bleh.. I need to start using unit tests
@jAndy I think its maybe they dont care to, you see the same alot of times with PHP
alot of people still dont take JS seriously
 
8:11 PM
probably, its pretty aweful what people have done to the language :p
 
in my <select> how can i add an option in JS? a <option value="123">123</option>
 
get a room with 100 people who claim to be "good" in ecmascript, ask about the meaning of the variable object/activation objects for instance, I bet like anything 50% will fail
 
There is a const keyword in JavaScript… did anyone know that?
 
@Nyuszika7H: doesn't have a meaning except in ES5 strict mode, does it ?
 
8:16 PM
@jAndy Yep, you're right. In ES5 strict mode, if the variable is already declared, it throws a TypeError. Nice for checking if a variable is declared, in a try-catch block.
 
@Karem: var newopt = document.createElement('option'); document.getElementById('idoflist').appendChild(newopt);
@Karem: jQuery: $('<li>').appendTo('#idoflist');
 
In non-strict mode, it will only throw an error when trying to declare a built-in variable (i.e. window).
 
funniest thing in ES5 strict is that it throws if you try to use a "with" statement
:p
 
really?
not for me
 
yes
 
8:18 PM
@jAndy its an select option not li
 
@Karem: oh sorry, then replace the ul with select :)
@Nyuszika7H: well it should throw by the spec
 
@jAndy in Chrome's console, it won't
 
"use strict";
var x = 17;
with (obj) // !!! syntax error
{
  // If this weren't strict mode, would this be var x, or
  // would it instead be obj.x?  It's impossible in general
  // to say without running the code, so the name can't be
  // optimized.
  x;
}
thats on MDC
 
Chrome has no strict mode... yet
 
they removed with because its slow & confusing
 
8:21 PM
@IvoWetzel but const seems to behave differently when including 'use strict';
 
they have some "things" of ES5 implemented
 
<?php
    foreach($_POST['myVeryLongFormNameThisIsReallyLong'] as $myForm) {
        …
    }
?>
 
drum roll drum roll jQuery 1.5.0 was just released drum roll
 
...!
Does jQuery 1.5 finally make ES5 obsolete?
 
I always thought jQuery is ES6
 
8:26 PM
oh nice
 
that was a fast rc
 
@jAndy Hm I guess jQuery is DOM 4.0
 
I really appreciated that they try to marshall anything which is natively available through. I guess Resig had a hard time to do that, especially with querySelector(All) in reference to Sizzle
well.. .sub() really sounds unnessary tho :p
 
8:32 PM
$(msg).find('div').attr('id') will this give me the div's id that are inside msg ?
 
@Karem: depends what msg is
 
its html
 
@Karem: then it will find all div nodes in that markup and return the id value from the very first match
 
theres only 1
and how could you specify a specific div in case if there were more divs
 
do the divs have any identifiers?
if you want the value of a specific one, you can select them $("#DivId")
 
8:39 PM
but its inside msg
 
if they have an id attribute set.
shouldnt matter
 
@Karem: in many, many ways. id, class, index, are the most common
 
id's should only be assigned once anyway
 
I like the new ajax converter callbacks in 1.5.0
that looks really cool
 
inside the success i have var aID = $(msg).find('div').attr('id'); and then i have a alert(aID) i get undefined
 
8:44 PM
@Karem: it's pretty impossible to tell whats wrong here, I suggest you post a question on SO with your complete code & markup
 
@Karem $(msg).find('div:eq(number)').attr('id'); Please note that it's zero-based, so first is 0, second is 1 and so on.
 
I really don't understand why there still isn't a callback to access XHRs readystate3 in jQuerys .ajax()
 
@jAndy what's readyState 3?
 
@Nyuszika7H: it's the suched called "interactive mode"
 
> Once the HTTP response content begins to load, the readyState property of the XMLHttpRequest object should be assigned a value of 3. XMLHttpRequest - Wikipedia
 
8:55 PM
@Nyuszika7H: when receiving data on a XMLHttpRequest, almost all browsers let you access the data while receiving nowadays
so while receiving data, readyState(3) is fired several times with the data that was received so far
when all data was received it's set to 4.. thats the point where jQuerys success callback fires
I actually couldn't think of a browser which does not allow it.. even IE8+ can do it
 
0
Q: jQuery: Is req.readyState == 3 possible?

I am a big fan of jQuery and I use it for 95% of all my Javascript needs. However, I am a loading a live page using a COMET method; where in Javascript I retreive the data using AJAX at the point where req.readyState == 3. I was curious if it's possible to do this with jQuery $.ajax too (I couldn...

1
Q: How to load an ajax (jquery) request response progressively without waiting for it to finish?

SebastianI want to make a form that will use jquery to submit a list of keyword to a php file, this file could take a lot of time to load depending on the size of the keywords list. What I want to do is to load the php response into a div or container in real time without using iframes. All the ajax req...

 
@Nyuszika7H: sure, you can use / overwrite the XHR object yourself, but thats not the point
 
Look!
> This example is from w3schools.com. (stackoverflow.com/questions/2751853/…)
 
I wrote my own plugin which just extends .ajax() with a interactive callback. I just can't understand why they don't abstract it there themself
but well, it looks like the new introduced transport hooks could make things easier to implement it
 
<html>
<body>

<?php
function writeName()
{
echo "Kai Jim Refsnes";
}

echo "My name is ";
writeName();
?>

</body>
</html>
example from W3Schools… that's really bad.
sorry I know it's the JS room… just one more!
function add($x,$y)
{
$total=$x+$y;
return $total;
}
awww.
 
9:10 PM
haha
nice
 
@jAndy do you know who made W3Fools?
 
I have no idea
 
@jAndy the jQuery team!!!
 
0
Q: How to make this part of my layout work?

AlexisHow can I make this part of my layout work? I can't work out how to fix this. Its become so messy and confusing. <div style='width: 900px; margin:0 auto'><div style='float: left; width: 312px; height: 197px; background-color: #A857BC'></div> <div style='float: left;...

 
hehe, yay sounds reasonable. W3Schools really sucks ass
 
9:12 PM
@jAndy I've seen that on Lifehacker
 
eeekk @IvoWetzel
 
> When you search for help online, one of the first results is often W3Schools (which I'm explicitly not linking to here). The short version is that it sucks. It's full of errors, it's missing information, and while it's not 100% useless it isn't a good resource. Avoid it. For the long version, visit W3Fools, a site put together by the jQuery team (and some other helpers).
 
I guess 99,9% of all SO readers close that kind of question below 0.5 seconds
 
that is very phalic ivo.
 
@jAndy And here's the article: lifehacker.com/5743401/…
function make(what) {
    alert('Making a cup of ' + what + '…');
    setTimeout(function() {
        alert('Done! Here you go, your cup of ' + what + '!');
    }, 2000);
    // This is only available for people who requested **one** cup of something before
    function make(what, amount) {
        if (!amount) amount = 1;
        cups = 'cup' + (amount == 1) ? '' : 's';
        alert('Making ' + amount + ' ' + cups + ' of ' + what + '…');
        setTimeout(function() {
            alert('Here you go, your ' + amount + ' ' + cups + ' of ' + what + '!');
Another reason why I like PHP better in some cases…
is looking for something like setTimeout(), but for PHP
 
9:24 PM
2
Q: How to convert anonymous function to a regular one?

AngryHackerTotal JS noob here. I have the following line that implements the jQuery Slider: <script type="text/javascript"> $(document).ready(function () { $("#wheelLeft").slider({ orientation: 'vertical', value: 37, min: -100, max: 100, ...

Should I cry?
 
he said he was a noob :S
 
0
A: How to convert anonymous function to a regular one?

Nyuszika7H$(document).ready(function myFunction() { // do something });

 
>><<
 
@david He has 7k rep
If he can't figure out something easy like that on his own... then sorry
 
ahaha, from asking 345 questions
 
9:28 PM
Another reason to reduce rep gain on questions by a factor of 10...
 
I'll admit, i've been tempted to ask really simple questions just as a way to farm rep... but my sense of morality keeps stopping me
 
So, here's my JS vs. PHP comparsion (sorry for double-post):
JavaScript
function make(what) {
    alert('Making a cup of ' + what + '…');
    setTimeout(function() {
        alert('Done! Here you go, your cup of ' + what + '!');
    }, 2000);
    // This is only available for people who requested **one** cup of something before
    function makeMore(what, amount) {
        if (!amount) amount = 1;
        cups = 'cup' + (amount == 1) ? '' : 's';
        alert('Making ' + amount + ' ' + cups + ' of ' + what + '…');
        setTimeout(function() {
            alert('Here you go, your ' + amount + ' ' + cups + ' of ' + what + '!');
PHP
 
@Nyuszika7H Your inner make function won't override the outer one
 
@IvoWetzel Hmm, I should make another function then. Changed it to makeMore.
 
could do window.make = blar
makemore won't be available outside make either
 
9:34 PM
the function declarations are evaluated before any code is run, the first make will be put in global scope and the second one in the scope of the first one
 
it's trapped!
 
@IvoWetzel Hmm, it works in PHP.
 
ahahahaha
 
@Nyuszika7H PHP !== JS :)
 
Hmm, window.makeMore = function(what, amount) may be fine.
 
9:35 PM
indeed
 
@IvoWetzel will window.make override the original make()?
 
it will also only be available after the execution of make
@Nyuszika7H If you assign to it, then yes
 
@IvoWetzel I know, that's the desired behavior.
@IvoWetzel nice :) now posting the same in PHP
 
a cleaner way then doing window.make = ... would be to simply override the outer make with a function expression
function make() {make = function(){return 4}; return 2}
undefined
make()
2
make()
4
 
function make($what) {
    echo 'Making a cup of '.$what.'…<br/>';
    sleep(2000);
    echo 'Done! Here you go, your cup of '.$what.'!';
    // This is only available for people who requested **one** cup of something before
    function make($what, $amount = 1) {
        $cups = ($amount == 1) ? 'cup' : 'cups';
        echo "Making $amount $cups of $what…<br/>";
        sleep(1000); // yeah, now it's 2x faster!
        echo "Here you go, your $amount $cups of $what!";
    }
}
This is the PHP code. I've used single quotes at the beginning, because I only needed to concat $what.
 
9:40 PM
0
Q: Image resized when page displayed inside of jQuery Dialog and iFrame

jon3lazeI am having weird display issues using the jQuery dialog with an iframe in Firefox and IE8. I have tested the page itself outside of the iframe and the problems are not reproduced, only when inside of the jQuery dialog. Issue image actual size: 300x225 image display size: 400x300 Body Code ...

 
@nyuszika7h so you're saying you like php better because it lets you use sleep()?
 
@david Not because sleep(), two reasons:
All variables begin with $, so you this code:
$foo = 'foobar';
echo "Foo is $foo";
will output: Foo is foobar
You can either escape the $ or use single quotes if you don't want to do that.
Now, which is more readable:
'My name is ' . $name . ' and I\'m ' . $age . '. I\'ve got a ' . $thing . ' in my ' . $something . '.'

"My name is $name, and I'm $age. I've got a $thing in my $something.";
This is one reason. Another reason is, in JS, you would do this:
function make(what) {
    if (!what) what = 'coffee';
    alert('Making a cup of ' + what);
}
In PHP, you do this:
function make($what = 'coffee') {
    echo 'Making a cup of ' . $what;
}
 
Hold on a sec... I gonna port Python 3's format to JS...
 
:kitten comein
 
I think people tend to use the what || "coffee" construct
 
9:48 PM
yep
what = what || coffee
 
@david Yep, but what if you want to reuse it multiple times?
 
I'm still hoping for a ||= operator someday :P
 
@IvoWetzel :P
OK who thinks it's funny to star everything I say?
[ closed - duplicate of 253975 ] :P
 
Now really, who starred this?
3 hours ago, by Nyuszika7H
@coure06 You can use screen.availHeight, though it's better to use window.innerHeight. But you should make it 5 or 10 pixels smaller anyway, to avoid unneccessary scrollbars. Though it's better to use
 
9:53 PM
no idea
 
function setGlobal($var, value) {
    if (/^||=/.test(value)) {
        return window[$var] = window[$var] || value;
    }
    return window[$var] = value;
}
@IvoWetzel here you go :P
var myVar = 'foo';
setGlobal('myVar', '||=bar');
if (myVar == 'foo') alert('It works!');
 
?
Oh, that ||= ?
I wanted that to be a shortcut for foo = foo || bar
so one could do foo ||= bar
 
I know :P
-----------------------------
| JavaScript Alert      [X] |
-----------------------------
| It works!                 |
|                      [OK] |
-----------------------------
2
 
haha :D
 

« first day (107 days earlier)      last day (4851 days later) »