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
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
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
...