test

Javascript

MIT License

test

Download (right click, save as, rename as appropriate)

Embed

Tags:

test
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
function log() {
	console.log.apply(console, arguments)
}

function assert(code) {
	if (typeof(code) === "string") {
		if (!eval(code)) {
			log("FAIL: '" + code + "'");
			return false;
		}
	} else {
		log("Assert failed: need string");
		return false;
	}
	log("PASS: " + code);
	return true;
}

function assert(code, message) {
	if (typeof(code) === "string") {
		if (!eval(code)) {
			log("FAIL: '" + code + "'");
			return false;
		}
	} else {
		log("Assert failed: need string");
		return false;
	}
	log("PASS: " + code + " : " + message);
	return true;
}

document.write("document test");
log("console test");

/*Ninja js*/
function isNimble(){ return true; } 
var canFly = function(){ return true; }; 
window.isDeadly = function(){ return true; }; 
assert("isNimble() && canFly() && isDeadly()");

//simple recursion
function yell(n){
	return n > 0 ? yell(n-1) + "a" : "hiy";
}
assert("yell(4) == \"hiyaaaa\"");

//Anon function with recursion
var ninja = {
	yell: function(n) { 
		return n > 0 ? ninja.yell(n-1) + "a" : "hiy";
		} 
};
assert("ninja.yell(4) == \"hiyaaaa\"");

//redefining ninja fucks shit up
var samurai = { yell: ninja.yell };
var ninja = {}; //redefine ninja
try { 
	samurai.yell(4);
} catch(e){ 
	log( "ninja.yell is fuucked " + e );
}

//named anonymous function
var ninja = {
	yell: function yell(n) { //anonymouse named function, buste
		return n > 0 ? ninja.yell(n-1) + "a" : "hiy";
	}
};
var samurai2 = { yell: ninja.yell };
assert("ninja.yell(4) == \"hiyaaaa\"");
assert("samurai2.yell(4) == \"hiyaaaa\"");

//arguments.callee
var ninja = { 
	yell: function(n) { //arguments.callee is the function itself
		return n > 0 ? arguments.callee(n-1) + "a" : "hiy";
		} 
};
assert("ninja.yell(4) == \"hiyaaaa\"");

//Functions as objects
var obj = {}; 
var fn = function(){};
assert("obj && fn", "both obj and fn are defined and exist");

//functions and objects can have properties
var obj = {}; 
var fn = function(){};
obj.prop = "some value";
fn.prop = "some value";
assert("obj.prop == fn.prop", "both obj and fn have properties");

//Storing functions
var store = {
	id: 1,
	cache: {},
	add: function(fn) {
		if (fn.id === undefined) {
			fn.id = store.id++;
			return !!(store.cache[fn.id] = fn);
		}
	}
};
function ninja(){}
assert("store.add(ninja)", "function added safely");
assert("!store.add(ninja)", "function added once");

function getElements( name ) { 
	return getElements.cache[ name ] = getElements.cache[ name ] ||
	document.getElementsByTagName( name );
}
getElements.cache = {};


var katana = { 
	isSharp: true, 
	use: function(){
		this.isSharp = !this.isSharp;
		} 
};
katana.use();
assert("!katana.isSharp", "Verify the value of isSharp has been changed." );


//Closure, names doesn't constantly get initialised and doesn't have to be a glbal var
var digit_name = function() {
	var names = ['zero', 'one', 'two' , 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
	return function(n) {
		return names[n];
	};
}();

alert(digit_name(3)); // 'three'