david-mark-llc / jessie

Javascript cross-browser library builder enabling (real) progressive enhancement
http://jessie.herokuapp.com
39 stars 13 forks source link

isHostObjectProperty #254

Closed web-bert closed 9 years ago

web-bert commented 10 years ago

I wanted to add a new function to check if the user is online, and this is the code I was going to do:

var isOnline;

if(isHostObjectProperty(global, "navigator") && isHostObjectProperty(global.navigator, "onLine")) {
    isOnline = function() {
        return global.navigator.onLine;
    };
}

but isHostObjectProperty returns false because the typeof check returns "boolean". Therefore should this function be renamed to:

jessie.isHostObjectPropertyAnObject()

As that shows what it actually does? It would be nice to then change isHostObjectProperty to just check for the existence of a property on an object (as that is what I would expect it to so), then it can be re-used by app code?

david-mark commented 10 years ago

Is Host "Object Property". Meaning the host object property references an object.

Sorry for any confusion.

web-bert commented 10 years ago

Ah I see!

Is it worth adding something like isObjectProperty, or leave apps to use typeof to check themselves?

On 22 Apr 2014, at 19:03, David Mark notifications@github.com wrote:

Is Host "Object Property". Meaning the host object property references an object.

Sorry for any confusion.

— Reply to this email directly or view it on GitHub.

david-mark commented 10 years ago

Use typeof where there is no ambiguity (e.g. booleans, strings, etc.) The isHost* functions are for host object properties that reference objects, which are known to vary their typeof result. isHostMethod specifically looks for host methods (which can be safely called), whereas isHostObjectProperty allows any sort of object (which can be referenced in any way), excluding ActiveX methods (which can only be safely called). The one used depends on what your code will do with the object. Choosing the wrong one can blow up (which makes it odd that copycat libraries merge these two into one).