【发布时间】:2016-05-02 17:32:25
【问题描述】:
我正在阅读closure library的代码段,我在那里看到了这段代码:
/**
* Gets the document object being used by the dom library.
* @return {!Document} Document object.
*/
goog.dom.getDocument = function() {
return document;
};
为什么我们将文档引用包装在 getter 方法中?文档不是全局对象吗?
【问题讨论】:
-
因此您可以模拟出来(替换方法)以进行测试并提供假文档。或者,任何时候,只需拆分底层实现。
-
@Sushanth-- 我不明白,即使这样,该方法也会返回修改后的值而不是正确的值,它的防御性如何?
-
哎呀..我的错。我忽略了它
标签: javascript oop tdd google-closure-library