Javascript
CA
Fixing the JavaScript typeof operator
[]
JavaScript, JavaScript
Working with JavaScript’s typeof operator is a bit like operating a clapped-out old car (or an early model Dell Inspiron).
typeof (2) //"number" typeof(2) //"number" typeof ("a", 3) //"number" typeof (1 + 1) //"number"What does it return?
var a; typeof a; //"undefined" typeof b; //"undefined" alert(a); //undefined alert(b); //ReferenceErrorOther than that, typeof is just not very discriminating.
Because of the limited usefulness of the typeof operator, duck-typing is popular in JavaScript.
"); } }Let’s run it, first with duck-typing…jsonParseIt(function() {return JSON && (typeof JSON.parse == "function")}) //"I'm not really JSON - fail!"
['fixing'
'typeof'
'objects'
'global'
'function'
'instanceof'
'javascript'
'json'
'return'
'object'
'totype'
'operator']