我如何导出功能变量

时间:2022-08-04 23:06:35

I want to export variable Code looks like:

我想导出变量代码看起来像:

let url = '';
module.exports = Object.freeze({
  url: url
});

I would to have possibility to edit property that it can be accesses by dot.

我有可能编辑可以通过点访问的属性。

ObjectWhichHasAboveCode.url = "http://help.me"

Can I achieve it in any functional way? Override assign operator?

我可以用任何功能方式实现它吗?覆盖分配运算符?

I tried;

1) url: () => url
2) url: {
    value: url,
    writable: true
   }

Is it possible to achieve it somehow?

是否有可能以某种方式实现它?

Second question:

Can I mix exporting Object.freeze methods together with let variable?

我可以将let.freeze方法与let变量混合使用吗?

[EDIT] Syntax error

[编辑]语法错误

2 个解决方案

#1


0  

If I understand correctly what you are looking for is the module pattern. You want method implementations to be frozen on the object, but you do not want the state of the object to be entirely immutable.

如果我正确理解您正在寻找的是模块模式。您希望在对象上冻结方法实现,但您不希望对象的状态完全不可变。

Here's what could be done:

这是可以做的:

module.exports = (function () {
  var url;

  return Object.freeze({
    get url() { return url; },
    setUrl: function (value) { url = value; }  
  });

})();

The url would have to be set using theObject.setUrl('some url'), but all the other members would be immutable. The url could still be accessed using the dot notation (theObject.url).

必须使用Object.setUrl('some url')设置url,但所有其他成员都是不可变的。仍然可以使用点表示法(theObject.url)访问URL。

EDIT:

Actually, what you are looking for is Object.seal.

实际上,你要找的是Object.seal。

module.exports = (function () {
  var url;

  return Object.seal({
    get url() { return url; },
    set url(value) { url = value; }
  });

})(); 

#2


0  

Seal the object instead of freezing it:

密封对象而不是冻结它:

The Object.seal() method seals an object, preventing new properties from being added to it and marking all existing properties as non-configurable. Values of present properties can still be changed as long as they are writable.

Object.seal()方法密封对象,防止将新属性添加到对象中,并将所有现有属性标记为不可配置。只要它们是可写的,当前属性的值仍然可以改变。

module.exports = Object.seal({url});

#1


0  

If I understand correctly what you are looking for is the module pattern. You want method implementations to be frozen on the object, but you do not want the state of the object to be entirely immutable.

如果我正确理解您正在寻找的是模块模式。您希望在对象上冻结方法实现,但您不希望对象的状态完全不可变。

Here's what could be done:

这是可以做的:

module.exports = (function () {
  var url;

  return Object.freeze({
    get url() { return url; },
    setUrl: function (value) { url = value; }  
  });

})();

The url would have to be set using theObject.setUrl('some url'), but all the other members would be immutable. The url could still be accessed using the dot notation (theObject.url).

必须使用Object.setUrl('some url')设置url,但所有其他成员都是不可变的。仍然可以使用点表示法(theObject.url)访问URL。

EDIT:

Actually, what you are looking for is Object.seal.

实际上,你要找的是Object.seal。

module.exports = (function () {
  var url;

  return Object.seal({
    get url() { return url; },
    set url(value) { url = value; }
  });

})(); 

#2


0  

Seal the object instead of freezing it:

密封对象而不是冻结它:

The Object.seal() method seals an object, preventing new properties from being added to it and marking all existing properties as non-configurable. Values of present properties can still be changed as long as they are writable.

Object.seal()方法密封对象,防止将新属性添加到对象中,并将所有现有属性标记为不可配置。只要它们是可写的,当前属性的值仍然可以改变。

module.exports = Object.seal({url});