Javascript: Eredita + modificare array associativo?
-
01-10-2019 - |
Domanda
sto cercando un modo elegante per sovrascrivere i valori in un array associativo.
Ad esempio, dire che ho le mie opzioni di base come:
var base_options = {
hintText:"something",
borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED,
width: 200, height: LABEL_HEIGHT-4,
font: {fontSize:16}, left: 95
}
Voglio usare questo come base, ma essere in grado di sostituire alcuni elementi in questa base, caso per caso - per esempio, hintText sarebbe diverso per ogni elemento. Che cosa è un modo pulito ed elegante per ottenere una copia di questo array con alcuni parametri modificati?
Mi rendo conto che posso cambiare ogni singolo elemento, come in:
options.hintText = "new thing";
, ma ho il sospetto che ci sia un modo più elegante.
Soluzione
Si potrebbe utilizzare una classe base per incapsulare il comportamento WESTON proporre.
function Options(changed_options) {
this.hintText = "something";
this.borderStyle =Titanium.UI.INPUT_BORDERSTYLE_ROUNDED;
// ...
if(changed_options)
for(var prop in changed_options)
this[prop] = changed_options[prop];
}
var foo = new Options({ "hintText":"changed"});
Il lavoro dovrebbe.
Altri suggerimenti
Ho implementato questa funzione in alcuni dei miei progetti:
if (typeof Object.merge !== 'function') {
Object.merge = function (o1, o2) { // Function to merge all of the properties from one object into another
for(var i in o2) { o1[i] = o2[i]; }
return o1;
};
}
Così, ora posso usarlo come:
Object.merge(options, {hintText: "new thing", left: 55});
Per quanto riguarda la copia di oggetti, c'è già un buon StackOverflow discussione su questo.
Una cosa come questa?
var changed_options = {
hintText: "somethingElse",
font: {fontSize: 24}
}
for(var prop in changed_options)
base_options[prop] = changed_options[prop];
function merge(base, options) {
var result = {};
for (var k in base) if (base.hasOwnProperty(k)) {
result[k] = options[k] || base[k];
} // note, it will leave out properties that are in options, but not in base..
return result;
}
Se vi capita di essere utilizzando jQuery, si è dotato di un metodo di extend
sull'oggetto jQuery che fa questo.
Si potrebbe utilizzare il prototipo dell'oggetto per stabilire l'eredità, in questo modo:
function inherited_object(extra_properties){
for(var i in extra_properties){
this[i] = extra_properties[i];
}
}
function inherit_from(parent, extra_properties){
inherited_object.prototype = parent;
var obj = new inherited_object(extra_properties || {});
inherited_object.prototype = null;
return obj;
}
Quindi, se avete un po A
oggetto appena chiamare B = inherit_from(A, B_stuff)
e basta.
Un vantaggio è che, a causa A
è il prototipo di B
, cambiamenti fatti al A
si riflettono sulla B
.
var base_options = function() {
hintText:arguments[0],
borderStyle:arguments[1],
width:arguments[2],
height:arguments[3],
font:arguments[4]
left:arguments[5]
};
var thisObj = new base_option(blah, blah, blah, blah, blah);
Questo può sembrare eccessivo, ma poi si può aggiungere tutte le nuove istanze di un array e utilizzare un ciclo for per loro quando si vuole / bisogno di cambiare loro.