【发布时间】:2015-03-27 14:16:29
【问题描述】:
我有一个原型函数,我想在有限的范围内使用它,以便为它提供一个 jquery 插件。
//Prototype
function StringBuilder(str) {
this.value = str;
}
StringBuilder.prototype.append = function (str) {
this.value = this.value + str;
return this;
};
//jQuery plugin with Revealing module pattern
jQuery.NameOfThePlugin = (function () {
//i would like to be able to use StringBuilder only in this scope
helloWorld = new StringBuilder('Hello');
helloWorld.append(' World');
})(window);
这可能吗?
谢谢
【问题讨论】:
-
您是指
StringBuilder本身,还是只是StringBuilder.prototype.append?如果您的意思是构造函数本身,只需将声明移动到该范围内。如果你不那么不,你不能。您可以在该范围内使用普通函数。 -
@JamesAllardice 如果是一个问题,那么是的。我不知道该怎么做...
-
抱歉,我已经编辑了我的评论以使其更有意义。
标签: javascript jquery design-patterns prototype revealing-module-pattern