- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想在 JavaScript 对象创建后立即密封它们:
'use strict';
class Test {
}
const t = Object.seal(new Test());
t.p = true; // error!
有没有办法自动完成,如下所示?
Test.sealInstances = true // I wish sealInstances was real!
const t = new Test();
t.p = true; // error
我知道我可以做到:
function createTest() {
return Object.seal(new Test())
}
并在所有地方使用 createTest
但我更喜欢 new Test()
语法。
最佳答案
只需将 Object.seal
放入构造函数中即可:
'use strict';
class Test {
constructor() {
Object.seal(this);
}
}
const t1 = new Test();
const t2 = new Test();
try {
t1.p = 'p';
} catch(e) { console.log(e.message) }
try {
t2.z = 'z';
} catch(e) { console.log(e.message) }
关于javascript - 是否可以自动密封JS对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58470349/
当数据类扩展包含非抽象 open val 属性的密封类时,生成的子数据类包含与父类的私有(private)字段重复的私有(private)字段。 sealed class Foo(open val f
当封装一个.jar 文件(整个.jar,而不是特定的包)时,实际上封装了哪些包?它只是包含.class 文件的包,还是还包含父包和子包? 举个例子,假设我有一个包含单个 .class 文件 com.c
我可以很容易地为这样的密封案例类族一般派生一个编解码器: import io.circe._ import io.circe.generic.auto._ sealed trait Base case
我有一个类层次结构: class C1 { virtual object M1(); } class C2: C1 { override sealed object M1(); } class C3:
有什么区别: type MovieType = {| +blob?: string, +name: string, +url?: string |}; 和 type MovieType =
想象一个这样的 secret : apiVersion: v1 kind: Secret metadata: name: {{ include "test-cicd.fullname" . }}
因此,我的一位开发人员使用这样的函数为 Magento 电子商务网站制作自定义导航: getUrl() ?>">__('about') ?> 唯一的问题是它的输出如下: about 据我了解,如果同时
我是一名优秀的程序员,十分优秀!