cloying
cloying

Reputation: 365

What does this JavaScript notation do: Object[key](value)

I'm not really sure what's going on here, but in a nutshell I've seen this:

Object[key](value);

In line 1088 of bootstrap-datetimepicker:

  $.fn.datetimepicker = function ( option, val ) {
    return this.each(function () {
      var $this = $(this),
      data = $this.data('datetimepicker'),
      options = typeof option === 'object' && option;
      if (!data) {
        $this.data('datetimepicker', (data = new DateTimePicker(
          this, $.extend({}, $.fn.datetimepicker.defaults,options))));
      }
      // Line below:
      if (typeof option === 'string') data[option](val);
    });
  };

Would anyone be able to answer what is going on?

I thought maybe it was assigning the value to the key in the object but when I tried doing something similar in the developer console (working in chrome v.33) it doesn't work.

Upvotes: 2

Views: 134

Answers (4)

Nope
Nope

Reputation: 22329

To access properties of an object in JavaScript you can either use the dot notation. i.e: Object.property or the string notation (also called bracket notation) Object[property].

Both are valid, though the dot notation doesn't work with property names containing spaces for example, such as Object.property name is invalid, while Object['property name'] is valid.

Given your example, Object[key](value) you are accessing a property of which the name is stored in the key from the Object object. The property happens to be a method and you can execute it passing value as the parameter.

Imagine the object to look like this:

Object = {
    myProp: function(newValue){
        // do something with newValue
    }
}

It would be perfectly fine to call it using the string notation if the method name is stored in a variable:

var key = 'myProp';
Object[key](value);

or if you don't need a variable you can also call it directly using the dot notation:

Object.myProp(value);

Resources: MDN on Property Accessors


Upvotes: 1

goto-bus-stop
goto-bus-stop

Reputation: 11824

var property = 'method';
// multiple ways to access properties
object.method === object['method'] === object[property];
// and you can use any syntax to call the method
// These all call `object.method`:
object.method() === object['method']() === object[property]();

See also https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Member_Operators

Upvotes: 1

Antoine
Antoine

Reputation: 2807

Object is a Javascript object that you can declare like this:

var obj = {};

Then a property is created (whose name is contained in the key variable) with a function as its value:

var obj['myfunction'] = function() { alert('Hello!'); };

So now,you have a function stored in your object 'obj' in the 'myfunction' key.

Since it's a function you execute it using '()', which results in:

obj['myfunction']()

Upvotes: 3

andrew.fox
andrew.fox

Reputation: 7933

Maybe just a hack to do something like:

var method = "create";
var prop = new String();
var str = Object[method](prop);

So you invoke a method create with parameter prop.

Upvotes: 0

Related Questions