Pages
Categories
Archives
JsTweeterManiac
- Seems @WebReflection have too much free time http://bit.ly/beb2tV 3 weeks ago
- looks awesome RT @stshank: Great augmented reality (AR) concept video. How far will we accept brand ads into our lives? http://bit.ly/b7R32U 1 month ago
- Dion names Crockford Crocky :)RT @dalmaer: Crocky keeps on going w/ history & just abt gets 2 JavaScript at the end :) http://bit.ly/cE0trz 1 month ago
- RT @kangax:@infynyxx Actually `Function.prototype.bind` is part of ES5 (now finalized), and is more or less identical to Prototype's `bind`. 1 month ago
- meditating on http://bit.ly/7mKxDI it makes me crazy. I thought i know a lot abt JS inheritance. Need 2 print ECMA specifications n smoke it 1 month ago
- Code Doesn’t Exist Unless It’s Checked In http://bit.ly/4Ah2uI Version control is a part of GTD :) 2 months ago
- RT @kangax: Zakas is doing some extensive research on empty-string URLs behavior — http://bit.ly/5EDWvb What a nice mess we've got there. 2 months ago
- I have now really nice shortname http://bit.ly/djafarov . I think shortnames could be as nice business as domain names:) 2 months ago
- http://bit.ly/6PCND6 nice article about @reply spam on twitter 2 months ago
- Awesome! RT @Evangenieur Google App Engine JavaScript SDK http://www.appenginejs.org/ 2 months ago
-
RSS Links
-
Meta
JS features you better know 1
In scope of functions arguments is local variable provides some nice features we can use in our code. First you don’t need to define any parameters for a function. You can just use arguments and will get arguments passed to the function.
function sum(){ var ret = 0; for (var i = 0; i < arguments.length; ++i) { ret += arguments[i]; } return ret; } sum(1, 2, 3) //returns 6Worth noting though that although we use arguments like an array, it’s not an actual javascript Array — it’s just an object. So you can’t do join(), pop(), push(), slice() and so forth.(You can convert it to a real array if you want: “var argArray = Array.prototype.slice.call(arguments);” )
arguments.callee property refers to the function that is currently running. It provides a way for unnamed function to refer to itself. This allows to make nice recursions:
function fibonacci(){ if (arguments.length == 1 && arguments[0] > 2) { return arguments.callee(arguments[0] - 2, [0, 1]); } if (arguments[0] == 0) { return arguments[1]; } else { var len = arguments[1].length; return arguments.callee(arguments[0] - 1, arguments[1].concat(arguments[1][len - 1] + arguments[1][len - 2])); } } fibonacci(7) //returns [0, 1, 1, 2, 3, 5, 8]Notice that “callee” property allows to do recursion with anonymous functions.
arguments.callee.caller property refers to a method called your function. This could be useful for example if you want to forbid public class creating and allow it for Factory only:
function MyClass(){ if (arguments.callee.caller != MyFactory.createObject) { throw new Error("There is no public constructor for MyClass."); } this.myproperty = "hello world"; }