Javascript, adding things in curly bracket and definition

3.3k views Asked by At

i'm in the process of modifying this excellent javascript library for autocomplete-ing html input see here... as i need to pass some extraparameters to the Ajax call. Actually i'm looking for something non destructive so that it can manage a default behaviour of this lib, that will also be able to handle "no extraparameters".

so i think i will have a call like

myExtra = {b : 'b', c: 'c'} 
// or maybe a thing like myExtra = "{b : 'b', c: 'c'}" 
myAutoComplete = new Autocomplete('query', { serviceUrl:'autoComplete.rails'}, myExtra);

with a null test for myExtra that does nothing if null or 'add' the content of myExtra to the Ajax request, so that this part:

  new Ajax.Request(this.serviceUrl, {
    parameters: { query: this.currentValue , },
    onComplete: this.processResponse.bind(this),
    method: 'get'
  });

become something like when myExtra is not null or empty string:

  new Ajax.Request(this.serviceUrl, {
    parameters: { query: this.currentValue , b: 'b', c: 'c'},
    onComplete: this.processResponse.bind(this),
    method: 'get'
  });

as i'm brand new to web developpment, my trouble is that i do not know how to add curly bracktetted content {} to another one (and i'm not sure that i even understand well what are these {}...)

i think i want to do this king of things that Python accept, but i just do not know how Javascript can do that,

 >>> class A:
def __init__(self, data):
    self.data = data
 >>> a = A("my data")
 >>> dir(a)
 ['__doc__', '__init__', '__module__', 'data']
 >>> myExtra = "b='b';c='c'"
 >>> for x in myExtra.split(";"):
    var, value = x.split('=')
        setattr(a, var, value)

 >>> dir(a)
 ['__doc__', '__init__', '__module__', 'b', 'c', 'data']
 >>> print a.data, a.b, a.c
 my data 'b' 'c'

so please, if you have any pointers for understanding this, Thank you in advance.

2

There are 2 answers

0
Felix Kling On BEST ANSWER

As @Matt already said in his comment, this pair of curly bracket is an object literal and creates an object. It is the same as

var myExtra = new Object();
myExtra.b = 'b';
myExtra.c = 'c';

but much more concise and the preferred way of creating an object.

I think what you want to do is copying properties from one object to another. You can do this by iterating over all properties of one object, using a for...in [MDN] loop:

var a = {query: this.currentValue};
var b = {b : 'b', c: 'c'};

for(var prop in b) {
    if(b.hasOwnProperty(prop) { // safe guard (might not be necessary)
        a[prop] = b[prop];
    }
}

To learn more about objects have a look at the MDN JavaScript Guide - Working with Objects.

2
Steve On

If you are able to use jQuery you can use the extend method. To take your example you would have something like:

var myDefaultExtras = { b: 'b', c: 'c' };

And then in your method you can do:

var finalExtras = $.extend({}, myDefaultExtras, extras);

You would then use the values from finalExtras.