Pull to refresh

Functions are almost objects

Reading time1 min
Views787
I was impressed! Вроде бы все понятно и давно известно, но в голову не приходило.

	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 was safely added." );
	assert( !store.add( ninja ), "But it was only added once." );



Взято из Secrets of JavaScript ninja by John Resig
Tags:
Hubs:
Total votes 16: ↑6 and ↓10-4
Comments14

Articles