I am very new to JavaScript, so I am baffled by the following syntax:
if (isFunction(obj)){
for (key in obj) {
//do something
}
}
The isFunction method will return true if typeOf obj=="function". But what happens when you it says key in obj when obj is a function?
The
for..inloop iterates over the enumerable properties ofobj. Functions are objects, they have their own properties plus inherited properties from their [[prototype]] chain. See ECMA-262 §12.6.4.Also, don’t forget to declare variables that should be kept local.
To address only the enumerable properties on
objand not its inherited enumerable properties, it is usual to include a hasOwnProperty test: