Custom proxy detects exception data

Time:2021-9-16

//Modify a property value of an object

const validator = {
                set:function (target,prop,value){
                    if(prop === 'productNum'){
                        if(value > 200){
                            Throw new error ('out of stock ')
                        }
                    }
                    target[prop] = value
                }
            }
            var products = new Proxy({},validator);
            products.productNum = 300;
//Data for detecting object specific properties
            let data = [ //getProductList
            {    
                id:1,
                product:{
                    productName:'aaa',
                    productDesc:'this is white Lu'
                },
                stock:null
                
            },
            {
                id:2,
                product:{
                    productName:'bbb',
                    productDesc:'this is taylon lu'
                },
                stock:{
                    productColor:'yellow',
                    productNum:14,
                    id:2
                }
                
            },
            {
                id:3,
                product:{
                    productName:'ccc',
                    productDesc:'this is lu zhi'
                },
                stock:{
                    productColor:'black',
                    productNum:14,
                    id:3
                }
                
            },
            {
                id:4,
                product:{
                    productName:'ddd',
                    productDesc:'this is JR'
                },
                stock:{
                    productColor:'green',
                    productNum:null,
                    id:4
                }
                
            }
        ]
            const validatorData = {
                get:function (target,propKey,receiver){
                    let dataId = target.id;
                    if(propKey === 'stock'){
                        try {
                            if(target[propKey] === null){
                                Alert ('item '+ dataid +' database save object is null ')
                            
                            }else if(target[propKey].productNum === null){
                            Alert ('the inventory quantity of the '+ target [propkey]. ID +' data under the inventory table is null, please handle it in time ')
                            }    
                        }catch(ex){
                            throw new Error(ex)
                        }
                        
                    }
                }
            }
            data.forEach((item,index)=>{
                item = new Proxy(item,validatorData);
                Item.stock // detect the stock object
            })

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]