Pages
Categories
Archives
JsTweeterManiac
- first time was on conference using skype call(toll free number). Skype is really great! 2 weeks ago
- RT @creationix: Connect app on quad-core machine over a real network connection. 35231.05 Reqs/sec http://bit.ly/aCWnd5 #nodejs #connect 1 month ago
- some provelinks #V8 vs #php http://ow.ly/1Xbg4 vs #ruby http://ow.ly/1Xbhl vs #Perl http://ow.ly/1Xbii vs #python http://ow.ly/1Xbjk 1 month ago
- http://ow.ly/1X9Z8 about #nodejs: "It is much faster than Ruby, Python, or Perl." really??? 1 month ago
- dyuproject(openID+oAuth) http://bit.ly/cfLC1b + facebook-java-api http://bit.ly/CnjrW + native google login. 2 months ago
- Strange, but there are so many Java libs are not created yet. I need openID + oAuth(twitter) + Facebook connect. 2 months ago
- Who will come to chatroulet next to perverts? - Salesmans http://bit.ly/d6HWS1 Shit! 2 months ago
- backend development excites much more than frontend. Frontend is kind of a job. And job is boring:).... Just kidding. 2 months ago
- I luv Microsoft. So many differences in browsers makes my salary much bigger. IE9 will not work on XP. Means long live 2 IE7-8. Horray!!! 2 months ago
- The Big Bang Theory: Everybody want to be Sheldon, Nobody wanna be Leonard:) 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"; }