Difference of this between non strict and strict functions
by Arnaud Buchholz
HTML
<table width="100%" border="1" cellspacing="0" cellpadding="4">
<thead>
<th>candidate</th>
<th><i>non strict</i> this</th>
<th><i>strict</i> this</th>
</thead>
<tbody id="results" />
</table>
CSS
span.typeof {
margin-left: 1rem;
color: #888888;
font-style: italic;
}
tr.diff {
background-color: #FFAAAA;
}
JavaScript
function nonStrict () {
return this;
}
function strict () {
"use strict";
return this;
}
[
0,
"",
false,
null,
undefined,
123,
"string",
true,
{isObject: true}
].forEach(function (candidate) {
var nonStrictResult = nonStrict.call(candidate);
var strictResult = strict.call(candidate);
var tr = document.createElement("tr");
function addTd (value) {
var td = document.createElement("td");
var stringified;
if (value === window) {
stringified = "<window>";
} else {
stringified = JSON.stringify(value);
}
td.appendChild(document.createTextNode(stringified));
var typeSpan = td.appendChild(document.createElement("span"));
typeSpan.className = "typeof";
typeSpan.appendChild(document.createTextNode(typeof value));
tr.appendChild(td);
}
addTd(candidate);
addTd(nonStrictResult);
addTd(strictResult);
if (nonStrictResult !== strictResult) {
tr.className = "diff";
}
document.getElementById("results").appendChild(tr);
})