domenica, aprile 29, 2007

Javascript ninja

recently i found this post in meeblog:
http://blog.meebo.com/?page_id=254

scrolling down to the end there is a simple test..

I think that that test is a bit confused, very vague... probably they do on purpose

now i gave the *REAL* (imho) answer to their question,
because i think that they are a good exercise ^_^;;
I hope doing so don't cause problems (I don't want enemy)


1. When does div.setAttribute(”###”) not equal div.###?

this question is vague... here some right answer(on angle brackets my suppposition):



always because ### is not a valid javascript

[if ### is a placeholder for a word]
since div is not defined if ### contains only character valid for a variable (i.e not -)
the error returned is always ("div is not defined");
so they are not equal when ### contains not valid javascript character for a object property.



[if they means DomNode.setAttribute]
since setAttribute want 2 parameter (name and value)
and since the value here is not showned then the first should raise a not enought argument error
the second one simply do nothing so they are always different.



2. What’s the difference between these two statements:

a. var x = 3;
b. x = 3;

[in global scope] there are no difference
[elsewhere] the first one create a private variable instead the second use (or create) a global variable.




3. What’s the difference between:

a. !!(obj1 && obj2)
b. (obj1 && obj2)

al 2 expression return always the same error:obj1 is not defined


[if we suppose that obj1 and obj2 are defined]

AND [if they are in using in a test (for example in a if condition)]
they are always the same since the condition automatically cast them as boolean value

[elsewhere] if they are in an assignment statement the difference is pretty simple

in the first case the returned value is true if both obj1 and obj2 are defined and not equals to false,undefined,null,0 or ''
ELSE the return value of this expression is false

in the second case if obj1 is equal to false,undefined,null,0 or '' the result is obj1 else is obj2


4. Write a one-line piece of JavaScript code that concatenates all strings passed into a function:

function concatenate(/*any number of strings*/) {

var string = /*your one line here*/
return string;
}



since in one line you could write all the code you want(separating with ; )


you could do something like

var string  = [''];(function(a){for(var i=0,max=a.length;i<max;i++)string[string.length]=a[i];})(arguments);string=string.join('');


5. What do these two examples have in common?

Example 1:

var obj = document.getElementById(’adiv’);
document.getElementById(’adiv’).ptr = obj;

Example 2:

function assignClick() {
var el = document.createElement(’div’);

function handleClick() {
el.innerHTML = ‘clicked!’;
}

el.attachEvent(”onclick”, handleClick);
}


both do nothing,
notice that the first could gave an error if there is not an element in the dom with the name adiv or if the node exists but the function is called before the dom is totally parsed (before oncontentload),
the second give an error if executed in an
application/xhtml +xml if the browser is not firefox since innerHTML
should don't exist(firefox is weird here)