JavaScript Reference (Mozilla)
JavaScript Reference
Advanced JavaScript
JavaScript Examples by Code
Links
function.prototype.fncname = function() {
;//code
}
function onkey(e) {
var key = (window.event) ? event.keyCode : e.keyCode //2nd is firefox
alert(key)
}
window.document.onkeydown = onkey
window.document.onkeyup = onkey
--------------------------------------------------------------------------------
The "arguments" property of a Function
functionname.arguments.length
functionname.arguments.callee
functionname.arguments.caller (depricated)
Get then current function (returns entire code...):
arguments.callee
The reason for this property is so anonymous functions can be recursive,
like:
var x = new function("arg","if(arg>0)arguments.callee(--arg);");
To add .getname() method to all functions:
function function_getname () {
return(this.toString().split(' ')[1].split('(')[0])
}
Function.prototype.getName = function_getName //Function must begin uppercase 'F'
function x() {
alert(arguments.callee.getname())
}
function y() {
//self contained, no prototyping...
alert(arguments.callee.toString().split(' ')[1].split('(')[0])
}
shorter code:
Function.prototype.getname = function() {
return(this.toString().split(' ')[1].split('(')[0])
}
other method to parse:
var fnc = arguments.callee.toString()
var name = fnc.substring(fnc.indexOf(' ')+1,fnc.indexOf('('));
Mozilla supposedly has name property on functions, so:
arguments.callee.name returns the name...
Note that the format of function Object.toString() is implementation
dependant. The above code is tested with NN4 and IE5 and works but
might fail for other implementations of JavaScript.
The arguments.caller property is deprecated in JavaScript 1.3 and is no
longer used, but where it is, it specifies the name of the function that
called the currently executing function.
function example() {
args = arguments
argl = arguments.length
this.var0 = (argl>0)?args[0]:null
this.var1 = (argl>1)?args[1]:null
this.var2 = (argl>2)?args[2]:null
this.var3 = (argl>3)?args[3]:null
this.var4 = (argl>4)?args[4]:null
this.var5 = (argl>5)?args[5]:null
this.var6 = (argl>6)?args[6]:null
}
--------------------------------------------------------------------------------
JavaScript Shorthand
Variable Assignment
var a,b,c,d;
var a = b = c = d = 0;
Default-Value Shorthand
x = x || ''
function f(a,b) {
if (!b) b = 0;
return(a+b);
}
function f(a,b) {
b = b || 0;
return(a+b);
}
Logical OR Operator (||)
result = expression1 || expression2
If either or both expressions evaluate to True, result is True.
var objx = document.ieobj || document.nsobj
var a = b || c;
...sets a to b if b is true, otherwise it sets it to c.
var a = b && c;
...sets a to c if b is true, otherwise it sets it to b.
Through some experimentation on the Firebug console, if neither b or c
are true, then a gets set to b.
function x(y) {
y=y||0
if(!y) return(ERROR_CODE_NOARGUMENT)
return(y)
}
function x(y) {
if(!(y=y||0)) return(ERROR_CODE_NOARGUMENT)
return(y)
}
if(!y=y||0) - does NOT work because "!y" appears as invalid varaible
if(!(y=y||0)) - does work because "()" isolates the ! from variable name
Ternary Expressions
Conditional (Ternary) Operator (?:)
Returns one of two expressions depending on a condition.
test ? expression1 : expression2
The operator "returns" expression1 if the condition is true,
otherwise it returns expression2.
1) x = (x) ? x : ''
2)if (x==1) doSomething()
2) (x==1) ? doSomething()
Either expression can be a "comma expression"
Two expressions executed sequentially.
for(var i=0;i TRUE
('1' === 1) -> FALSE
--------------------------------------------------------------------------------
Notes
JScript uses the following rules for converting non-Boolean values to Boolean
values:
o All objects are considered true.
o Strings are considered false if and only if they are empty.
o null and undefined are considered false.
o Numbers are false if, and only if, they are 0.
For loop with Arrays:
for(i in arrx)
alert(i)
void expression | void(expression)
The void operator evaluates its expression, and returns undefined. It is most
useful in situations where you want an expression evaluated but do not want
the results visible to the remainder of the script.
--------------------------------------------------------------------------------
Swap two variables using XOR
http://betterexplained.com/articles/swap-two-variables-using-xor/
Most people would swap two variables x and y using a temporary variable, like
this:
tmp = x
x = y
y = tmp
You can use XOR to swap two variables without needing a temp:
x = x xor y
y = x xor y
x = x xor y
JavaScript Implementation:
x ^= y
y ^= x
x ^= y
The underlying principle of the XOR swap algorithm can be applied to any
reversible binary operation. Replacing XOR by addition and subtraction gives
a slightly different, but largely equivalent, formulation:
procedure AddSwap(var X, Y: integer);
begin
if X <> Y then begin
X := X + Y;
Y := X - Y;
X := X - Y
end
end