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.
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
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:To learn more about objects have a look at the MDN JavaScript Guide - Working with Objects.