Getter/setter on javascript array?
Is there a way to get a get/set behaviour on an array? I imagine something like this:开发者_C百科
var arr = ['one', 'two', 'three'];
var _arr = new Array();
for (var i = 0; i < arr.length; i++) {
arr[i].__defineGetter__('value',
function (index) {
//Do something
return _arr[index];
});
arr[i].__defineSetter__('value',
function (index, val) {
//Do something
_arr[index] = val;
});
}
Using Proxies, you can get the desired behavior:
var _arr = ['one', 'two', 'three'];
var accessCount = 0;
function doSomething() {
accessCount++;
}
var arr = new Proxy(_arr, {
get: function(target, name) {
doSomething();
return target[name];
}
});
function print(value) {
document.querySelector('pre').textContent += value + '\n';
}
print(accessCount); // 0
print(arr[0]); // 'one'
print(arr[1]); // 'two'
print(accessCount); // 2
print(arr.length); // 3
print(accessCount); // 3
print(arr.constructor); // 'function Array() { [native code] }'
<pre></pre>
The Proxy constructor will create an object wrapping our Array and use functions called traps to override basic behaviors. The get
function will be called for any property lookup, and doSomething()
before returning the value.
Proxies are an ES6 feature and are not supported in IE11 or lower. See browser compatibility list.
Array access is no different to normal property access. array[0]
means array['0']
, so you can define a property with name '0'
and intercept access to the first array item through that.
However, that does make it impractical for all but short, more-or-less-fixed-length Arrays. You can't define a property for “all names that happen to be integers” all in one go.
I looked up in John Resig's article JavaScript Getters And Setters, but his prototype example didn't work for me. After trying out some alternatives, I found one that seemed to work. You can use Array.prototype.__defineGetter__
in the following way:
Array.prototype.__defineGetter__("sum", function sum(){
var r = 0, a = this, i = a.length - 1;
do {
r += a[i];
i -= 1;
} while (i >= 0);
return r;
});
var asdf = [1, 2, 3, 4];
asdf.sum; //returns 10
Worked for me in Chrome and Firefox.
I hope it helps.
Object.extend(Array.prototype, {
_each: function(iterator) {
for (var i = 0; i < this.length; i++)
iterator(this[i]);
},
clear: function() {
this.length = 0;
return this;
},
first: function() {
return this[0];
},
last: function() {
return this[this.length - 1];
},
compact: function() {
return this.select(function(value) {
return value != undefined || value != null;
}
);
},
flatten: function() {
return this.inject([], function(array, value) {
return array.concat(value.constructor == Array ?
value.flatten() : [value]);
}
);
},
without: function() {
var values = $A(arguments);
return this.select(function(value) {
return !values.include(value);
}
);
},
indexOf: function(object) {
for (var i = 0; i < this.length; i++)
if (this[i] == object) return i;
return -1;
},
reverse: function(inline) {
return (inline !== false ? this : this.toArray())._reverse();
},
shift: function() {
var result = this[0];
for (var i = 0; i < this.length - 1; i++)
this[i] = this[i + 1];
this.length--;
return result;
},
inspect: function() {
return '[' + this.map(Object.inspect).join(', ') + ']';
}
}
);
It is possible to define Getters and Setters for JavaScript arrays. But you can not have accessors and values at the same time. See the Mozilla documentation:
It is not possible to simultaneously have a getter bound to a property and have that property actually hold a value
So if you define accessors for an array you need to have a second array for the actual value. The following example illustrates it.
//
// Poor man's prepare for querySelector.
//
// Example:
// var query = prepare ('#modeler table[data-id=?] tr[data-id=?]');
// query[0] = entity;
// query[1] = attribute;
// var src = document.querySelector(query);
//
var prepare;
{
let r = /^([^?]+)\?(.+)$/; // Regular expression to split the query
prepare = function (query, base)
{
if (!base) base = document;
var q = []; // List of query fragments
var qi = 0; // Query fragment index
var v = []; // List of values
var vi = 0; // Value index
var a = []; // Array containing setters and getters
var m; // Regular expression match
while (query) {
m = r.exec (query);
if (m && m[2]) {
q[qi++] = m[1];
query = m[2];
(function (qi, vi) {
Object.defineProperty (a, vi, {
get: function() { return v[vi]; },
set: function(val) { v[vi] = val; q[qi] = JSON.stringify(val); }});
})(qi++, vi++);
} else {
q[qi++] = query;
query = null;
}
}
a.toString = function () { return q.join(''); }
return a;
}
}
The code uses three arrays:
- one for the actual values,
- one for the JSON encoded values
- and one for the accessors.
The array with the accessors is returned to the caller. When a set
is called by assigning a value to the array element, the arrays containing the plain and encoded values are updated. When get
gets called, it returns just the plain value. And toString
returns the whole query containing the encoded values.
But as others have stated already: this makes only sense, when the size of the array is constant. You can modify the existing elements of the array but you can not add additional elements.
Why not create a new class for the inner objects?
var a = new Car();
function Car()
{
// here create the setters or getters necessary
}
And then,
arr = new Array[a, new Car()]
I think you get the idea.
It is possible to create setters for each element of an array, but there is one limitation: you would not be able to directly set array elements for indexes that are outside the initialized region (e.g. myArray[2] = ... // wouldn't work if myArray.length < 2
) Using the Array.prototype functions will work. (e.g. push, pop, splice, shift, unshift.) I give an example of how to accomplish this here.
this is the way I do things. You will have to tweak the Prototype Creation (I removed a bit from my Version). But this will give you the default getter / setter behavior I am used to in other Class-Based Languages. Defining a Getter and no Setter means that writing to the element will be ignored...
Hope this helps.
function Game () {
var that = this;
this._levels = [[1,2,3],[2,3,4],[4,5,6]];
var self = {
levels: [],
get levels () {
return that._levels;
},
setLevels: function(what) {
that._levels = what;
// do stuff here with
// that._levels
}
};
Object.freeze(self.levels);
return self;
}
This gives me the expected behavior of:
var g = new Game()
g.levels
/// --> [[1,2,3],[2,3,4],[4,5,6]]
g.levels[0]
/// --> [1,2,3]
Taking up the critizism from dmvaldman: Writing should now be impossible. I rewrote the code to 1)not use depracated elements (__ defineGetter __) and 2) not accept any writing (that is: uncontrolled writing) to the levels element. An example setter is included. (I had to add spacing to __ defineGetter because of markdown)
From dmvaldmans request:
g.levels[0] = [2,3,4];
g.levels;
/// --> [[1,2,3],[2,3,4],[4,5,6]]
//using setter
g.setLevels([g.levels, g.levels, 1,2,3,[9]]);
g.levels;
/// --> [[[1,2,3],[2,3,4],[4,5,6]],[[1,2,3],[2,3,4],[4,5,6]], ....]
//using setLevels
g.setLevels([2,3,4]);
g.levels;
/// --> [2,3,4]
This answer is just an extension to the solution based on Proxy. See the solution with proxy, in that only get is mentioned but we can also use set as I am showing here.
Notice: 3rd argument in set can carry the value...
The code is self explanatory.
var _arr = ['one', 'two', 'three'];
var accessCount = 0;
function doSomething() {
accessCount++;
}
var arr = new Proxy(_arr, {
get: function(target, name) {
doSomething();
return target[name];
},
set: function(target, name, val) { doSomething(); target[name] = val; }
});
function print(value) {
document.querySelector('pre').textContent += value + '\n';
}
print(accessCount); // 0
print(arr[0]); // 'one'
print(accessCount); // 1
arr[1] = 10;
print(accessCount); // 2
print(arr[1]); // 10
in typescript i made this
export class ArraySetter<T = any> extends Array<T>{
set add(val: T) {
this.push(val);
}
get sum() {
//use this to access array
return 'anything you want'
}
}
usage
let tasks = new ArraySetter();
tasks.add = Task1();
tasks.add = Task2();
tasks.add = Task3();
this is just the idea, you can add whatever functionality needed.
The correct and modern way would be to proxy the array as accepted answers have already noted.
That being said, I only post the alternative below, based on custom getters/setters, for some cases of backwards compatibility and as a proof of concept.
function observeArray(array, notify)
{
var methodInterceptor = function(array, notify) {
var interceptor = function(array, method, notify) {
return function() {
var initialLength = array.length;
var result = Array.prototype[method].apply(array, arguments);
if ('push' === method || 'unshift' === method || 'splice' === method)
{
itemInterceptor(array, initialLength, array.length, notify);
}
notify(null, null, method);
return result;
};
};
['push', 'pop', 'shift', 'unshift', 'splice'].forEach(function(method) {
array[method] = interceptor(array, method, notify);
});
return array;
};
var itemInterceptor = function(array, start, stop, notify) {
var interceptor = function(array, index) {
var key = String(index), val = array[index];
Object.defineProperty(array, key, {
get() {
return val;
},
set(value) {
if (val !== value)
{
val = value;
notify(val, index);
}
},
enumerable: true
});
};
for (var index=start; index<stop; ++index)
{
interceptor(array, index);
}
return array;
};
return itemInterceptor(methodInterceptor(array, notify), 0, array.length, notify);
}
var a = observeArray([1,2,3], function(item, index, method){console.log(method ? 'array modified by method '+method : 'item at '+index+' ('+item+') has been modified');});
console.log(JSON.stringify(a));
a[0] = 4;
console.log(JSON.stringify(a));
a.push(5);
console.log(JSON.stringify(a));
a.unshift(0);
console.log(JSON.stringify(a));
a[0] = 1;
console.log(JSON.stringify(a));
console.log(a[0]);
You can add whatever methods you like to an Array
, by adding them to Array.prototype
. Here's an example that adds a getter and setter
Array.prototype.get = function(index) {
return this[index];
}
Array.prototype.set = function(index, value) {
this[index] = value;
}
精彩评论