Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.7k views
in Technique[技术] by (71.8m points)

关于 js 数组 setter

需求:object.array[index] = value 时调用 setter


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

利用Proxy:

var arr = []
var proxyArr = new Proxy(arr, {    
    set: function(target, key, value) {    
        // do something    
        console.log(`key=${key}, value=${value}`);
        target[key] = value; 
    }
});


proxyArr[0] = 12;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...