javascript对象的扩展性限制只限于方法?

是否可以仅限制方法的可扩展性?

由于preventExtension()阻止添加属性和方法(因为方法也是属性).

什么是防止进一步添加方法的解决方案(并允许添加非方法属性)?

Object.preventExtensions(obj);

最佳答案

Is it possible to restrict the extensibility for methods only?

不可以.该语言不区分方法和数据属性.

What is the solution to prevent further addition of methods only ( and allow addition of non method property)?

您始终可以阻止扩展,但在此之前为某些属性定义setter和getter,允许设置为不可调用的值.

点赞