我正在传递访问器功能
let species_accessor = d => d.species;
给定一个对象
let alpha = {};
I would like to use whichever field ('species'
here) the accessor is defined on, to set that field in the object.
species_accessor(alpha) = 'unknown';
那不会飞。
console.log(alpha);
我忘了传递访问器函数,而只是传递字段名称
let species_field_name = 'species';
然后设置字段非常简单
let beta = {};
beta[species_field_name] = 'unknown';
console.log(beta);
但这很笨拙。
如何从访问器函数中提取字段名称,或者如何使用访问器进行设置而不是获取?
You can't do something like this in javascript. The closest thing to this is having your
species_accessor
take in a second argument which is the value you want to set the field to, so it becomes(d, v) => d.species = v
. Then, you can change the alpha object viaspecies_accessor(alpha, "unknown")
.用getter函数无法做到这一点。您将需要传递一个完整的附件,即一对吸气剂/吸气剂:
那你就可以