Skip to content Skip to sidebar Skip to footer

Meaning Of --> If (typeof Options === 'string' && Methods[options])

Can someone explain what the expression methods[options] evaluates in this code? $.fn[pluginname] = function(options) { if (typeof options === 'string' **&& methods

Solution 1:

methods[options] accesses the value in the methods object associated with a key equal to the string value of the options variable. If a truthy value is stored there, it would return true. e.g.,

var methods = {yes: 'hello'}
  var options = 'yes'
  (typeof options === 'string' && methods[options]) //evaluates as true

  options = 'no'
  (typeof options === 'string' && methods[options]) //evaluates as false 

The second one evaluates as false because methods['no'] returns undefined. However, in your question you said options is an object- if that's always true, then the first half of the boolean typeof options === 'string' will always be false and cause the whole expression to be false. I'd imagine that the plugin gives the option to pass in a string or object value for options.

Post a Comment for "Meaning Of --> If (typeof Options === 'string' && Methods[options])"