Pages
Categories
Archives
JsTweeterManiac
- #nodejs google plus fan page just crossed 1000 followers! http://t.co/IdbGqaXd 4 days ago
- #nodejs google plus fan page just crossed 800 followers http://t.co/IdbGqaXd ! 2 weeks ago
- hey @ryah {http://t.co/fdc8SCPR} you've done some cool stuff, I'm a fan via {http://t.co/2NPs9fNA} 2 weeks ago
- node.js makes programming fun again https://t.co/qgZwpOzL 3 weeks ago
- core #nodejs in #coffeescript? How about debugging this? http://t.co/iICMV30r 1 month ago
- 1999:Java - is for small projects,use C. 2005:Ruby - is for small projects,use Java. 2011:node.js - is for small projects, use Ruby for big 1 month ago
- >npm xmas 1 month ago
- WebStorm 3 officially out. With #nodejs and #coffeescriptsupport http://t.co/kCrF8Ba4 1 month ago
- Behold of #eval fanpage join #javascript #evil legions! https://t.co/9b2NotMs 1 month ago
- Position: fixed revisited http://t.co/jJ0fPsoy 1 month 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"; }