toop
toop

Reputation: 25

javascript - function with dot notation using concatenation and array value

<script src="modernizr-1.7.min.js"></script>    
var modernizr_fields = new Array("canvas","video","webgl");     
for (i=0; i < modernizr_fields.length; i++) {
    document.writeln(modernizr_fields[i] + " ");    
    if (Modernizr + "." + modernizr_fields[i])
        document.writeln("true");   
    else
        document.writeln("false");
    document.writeln("<br>");       
}

I know the problem is with this line: "if (Modernizr + "." + modernizr_fields[i])" as it is always evaluating to "true"

Please help with my syntax.

Upvotes: 1

Views: 1529

Answers (1)

Alnitak
Alnitak

Reputation: 339816

You need:

if (Modernizr[modernizr_fields[i]]) {
     ...
}

The format obj.field only works with literal field names, if field is instead a variable you have you use obj[field]

Upvotes: 1

Related Questions