June 2011
3 posts
3 tags
JavaScript: test if a property is a function
var isFunction = function(property) { return typeof property === 'function'; } var x = { qq : 'QQ' }; x.doStuff = function() { "stuff happened"; }; console.log(isFunction(x.doStuff)); // true console.log(isFunction(x.hasOwnProperty)); // true console.log(isFunction(x.qq)); // false
Jun 24th
3 notes
2 tags
JavaScript Type Coercion
JavaScript: 2 + "256" // "2256" 2 - "256" // -254 typeof (+"256") // number typeof (-"256") // number 2 + +"256" // 258 2 - -"256" // 258 typeof -"" // number typeof +"" // number +"" === -"" // true +"" === 0 // true "" === 0 // false
Jun 19th
7 notes
4 tags
JavaScript: isInt function
var isInt = function(x) { return x === parseInt(x, 10) && !isNaN(x); } console.log(isInt(755)); // true console.log(isInt(-644)); // true console.log(isInt(0)); // true console.log(isInt(3.14)); // false console.log(isInt("256")); // false console.log(isInt("-256")); // false console.log(isInt("7.62")); // false console.log(isInt("-0.14")); // false ...
Jun 17th
2 notes