You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
consttargetObject={prop: "value"};consthandler={set(target,key,value){thrownewError("Object is immutable.");},deleteProperty(target,key){thrownewError("Object is immutable.");},};constimmutableObject=newProxy(targetObject,handler);// 以下操作都会抛出错误immutableObject.prop="newValue";deleteimmutableObject.prop;
The text was updated successfully, but these errors were encountered:
关键词:防止对象篡改
在 JavaScript 中,可以通过以下几种方式防止对象被篡改:
一、使用
Object.freeze()
功能:
示例:
二、使用
Object.seal()
功能:
示例:
三、使用
const
声明对象引用功能:
const
声明的变量不能被重新赋值,但对象本身的属性仍然可以被修改,除非使用上述冻结或密封的方法。示例:
四、使用代理(Proxy)进行拦截
功能:
示例:
The text was updated successfully, but these errors were encountered: