Having a Array like this:
var k =[1,2];
k.push(3);
redefine push like this:
function kPush(e){
this[this.length] = e * 2;
}
Object.defineProperty(k, 'push', {
get: function(){return kPush;}
});
And calling using
k.push(4);
document.write(k);
Gives:
var k =[1,2]; k.push(3); function kPush(e){ this[this.length] = e * 2; } Object.defineProperty(k, 'push', { get: function(){return kPush;} }); k.push(4); document.write(k);
Gives:
1,2,3,8
Everythings fine. Now I need to overwride splice
but its never called.
Object.defineProperty(k, 'splice', {
get: function(){return kSplice;}
});
But
console.debug(k.splice);
gives me function splice() { [native code] }
. <-- its native? But i have overwritten the property!
How to overwrite splice
?
var k =[1,2]; k.push(3); function kPush(e){ this[this.length] = e; } Object.defineProperty(k, 'push', { get: function(){return kPush;} }); Object.defineProperty(k, 'splice', { get: function(){return function(){ console.log("Splice"); Array.prototype.splice.apply(this,arguments); }} }); k.push(4); k.splice(1,1); document.write(k);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.