一组共享公共上下文的 Javascript 闭包被称为什么?

发布于 2024-09-05 19:18:01 字数 681 浏览 4 评论 0原文

我一直在尝试学习闭包(在 Javascript 中),在使用 C# 和 C++ 多年之后,这有点伤害我的大脑。我想我现在已经有了基本的了解,但有一件事困扰着我:我在这个知识探索中访问了很多网站,但我没有在任何地方看到一个词(甚至是一个简单的两个词短语)表示“一组共享公共执行上下文的 Javascript 闭包”。例如:

function CreateThingy (name, initialValue)
{
    var myName = name;
    var myValue = initialValue;

    var retObj = new Object;

    retObj.getName = function() { return myName; }
    retObj.getValue = function() { return myValue; }
    retObj.setValue = function(newValue) { myValue = newValue; }

    return retObj;
};

根据我的阅读,这似乎是实现数据隐藏的一种常见方法。 CreateThingy 返回的值当然是一个对象,但是作为该对象的属性的一组函数您会怎么称呼呢?每个都是一个闭包,但我想要一个可以用来描述(并思考)所有这些作为一个概念实体的名称,并且我宁愿使用一个普遍接受的名称,而不是组成一个名称。

谢谢!

——艾德

I've been trying to learn closures (in Javascript), which kind of hurts my brain after way too many years with C# and C++. I think I now have a basic understanding, but one thing bothers me: I've visited lots of websites in this Quest for Knowledge, and nowhere have I seen a word (or even a simple two-word phrase) that means "a set of Javascript closures that share a common execution context". For example:

function CreateThingy (name, initialValue)
{
    var myName = name;
    var myValue = initialValue;

    var retObj = new Object;

    retObj.getName = function() { return myName; }
    retObj.getValue = function() { return myValue; }
    retObj.setValue = function(newValue) { myValue = newValue; }

    return retObj;
};

From what I've read, this seems to be one common way of implementing data hiding. The value returned by CreateThingy is, of course, an object, but what would you call the set of functions which are properties of that object? Each one is a closure, but I'd like a name I can used to describe (and think about) all of them together as one conceptual entity, and I'd rather use a commonly accepted name than make one up.

Thanks!

-- Ed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遮了一弯 2024-09-12 19:18:01

Crockford 推广了术语“特权方法”来命名有权访问该范围的函数其构造函数(声明私有成员的地方)。

我将它们称为“一组共享公共封闭范围的 JavaScript 函数”。

您发布的短语中的“执行上下文”部分可能会在 执行上下文this 值,通常也称为“函数上下文” 或“对象上下文” em> 和函数作用域,这才是这里真正引用的内容。

Crockford popularized the term "privileged method" to name the functions that have access to the scope of its constructor (where the private members are declared).

I would call them "A set of JavaScript functions that share a common enclosing scope".

The "execution context" part of the phrase you post, can create confusion between the Execution Contexts described in the specification, the this value, which also is popularly known as "the function context" or "object context", and the function scope, which is really what is being referenced here.

标点 2024-09-12 19:18:01

您可以将其称为,但它只是一个包含一堆条目的哈希表,其中一些是函数(或闭包)。在 Javascript 中更惯用的方法是使用原型。

一些可以帮助您的链接:

You could call it a class, but it's just a hash table with a bunch of entries, some of which are functions (or closures). The more idiomatic way to do this in Javascript is through the use of prototypes.

Some links to help you:

终陌 2024-09-12 19:18:01

它接近模块模式。使用如下表达式:

var thingy = (function(specObj) {
    var my = {},
        privateVar = blah,
        someOtherPrivateVar = [];

    my.getSomeVal() {
        return specObj.someVal;
    }
    my.getAnotherVal() {
        return specObj.AnotherVal;
    }
}({
    someVal: "value",
    anotherVal: "foo"
}));

我使用对象文字进行初始化,因为使用命名对象将允许通过更改命名对象来更改事物的状态。还有其他选项可以做到这一点。

这是关于模块模式变体的另一个很好的链接:
http://www.adequatelygood.com/2010/3/ JavaScript-Module-Pattern-In-Depth

几乎不言而喻,但 Crockford 的 Javascript:优秀部分 包含对这种风格及其具体优点的良好处理。

Its close to the module pattern. Use an expression like so:

var thingy = (function(specObj) {
    var my = {},
        privateVar = blah,
        someOtherPrivateVar = [];

    my.getSomeVal() {
        return specObj.someVal;
    }
    my.getAnotherVal() {
        return specObj.AnotherVal;
    }
}({
    someVal: "value",
    anotherVal: "foo"
}));

I initialized with an object literal because using a named object would allow the state of thingy to be changed by changing the named object. There are other options to do this.

Here's another good link on variations of the module pattern:
http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth

Almost goes without saying, but Crockford's Javascript: The Good Parts contains a good treatment of this style and its specific benefits.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文