⬆️ ⬇️

Functions are almost objects

I was impressed! It seems that everything is clear and has long been known, but it did not occur to me.



	 var store = {
		 id: 1,
		 cache: {},
		 add: function (fn) {
			 if (! fn.uuid) {
				 fn.uuid = store.id ++;
				 return !! (store.cache [fn.uuid] = fn);
			 }
		 }
	 };

	 function ninja () {}

	 assert (store.add (ninja), "Function is safely added.");
	 assert (! store.add (ninja), "But it was only added once.");





Taken from Secrets of JavaScript ninja by John Resig


')

Source: https://habr.com/ru/post/88211/



All Articles