简体   繁体   English

命名空间javascript中的私有函数

[英]Private functions in namespaced javascript

I'm trying to define a way in which I can control the access to methods for the javascript we use at work (Part of a coding standards improvement drive). 我正在尝试定义一种方法,通过该方法可以控制对我们在工作中使用的javascript方法的访问(编码标准改进驱动器的一部分)。

The pattern I was planning to use was fine until I thought about what we do with 3rd party script methods. 在我考虑使用第三方脚本方法之前,我打算使用的模式很好。

How can I tweak my code to allow access to the private function from inside the jQuery method? 如何调整我的代码以允许从jQuery方法内部访问私有函数?

var NameSpace = new function () {

    // My private function I want to access.
    var privateFunction = function () {

    };

    this.publicFunction = function () {

        // I can access my private function here.
        privateFunction();

        jQuery(window).resize(function () {

            // But not here :(
            privateFunction();

        });
    };
};
    // I can access my private function here.
    privateFunction();
    jQuery(window).resize(function () {
        // But not here :(
        privateFunction();
    });

Yes you can. 是的你可以。 JavaScript is statically scoped. JavaScript是静态作用域的。 You can access all the variables and functions of each enclosing scope in that resize callback, including privateFunction (as long as you haven't shadowed it by defining a privateFunction in a nested scope). 您可以访问该调整大小的回调中每个封闭作用域的所有变量和函数,包括privateFunction (只要您没有通过在嵌套作用域中定义privateFunction来掩盖它)。

What you can't do is access any of those local variables outside the enclosing function. 您不能做的是访问封闭函数之外的任何局部变量。

I'm trying to define a way in which I can control the access to methods for the javascript we use at work (Part of a coding standards improvement drive). 我正在尝试定义一种方法,通过该方法可以控制对我们在工作中使用的javascript方法的访问(编码标准改进驱动器的一部分)。

I would suggest that this is a pointless exercise. 我建议这是没有意义的练习。

Namespacing is good for avoiding unwanted name clashes. Namespacing可以避免不必要的名称冲突。 What truly 'private' members are for is strictly enforcing rigid security boundaries. 真正的“私有”成员的目的是严格执行严格的安全边界。 But this is JavaScript: you aren't running code with different access levels and sandboxing here, like you might in Java. 但这是JavaScript:您不会像在Java中那样在此处运行具有不同访问级别和沙箱的代码。 You don't have to blindly reproduce the security model of Java in JavaScript. 您不必盲目地在JavaScript中重现Java的安全模型。 Who is the 'attacker' here? 谁是这里的“攻击者”? Yourself? 你自己 Other coders in your team? 您团队中的其他编码员?

Data hiding and encapsulation is good practice, but you don't need to strictly enforce privateness to achieve this. 数据隐藏和封装是一种好习惯,但是您无需严格强制私有即可实现此目的。 Indeed, having real privates may make debugging and prototyping tasks more difficult. 确实,拥有真正的私人用户可能会使调试和原型制作任务变得更加困难。 Consider the Python-like approach where you simply mark members that aren't supposed to be used from outside, for example with a leading underscore. 考虑一下类似Python的方法,您可以简单地标记不应从外部使用的成员,例如使用前划线。 Anyone using a pseudo-private member knows they're doing something they shouldn't, and hopefully have a good temporary reason for doing so. 任何使用伪私有成员的人都知道他们正在做不应做的事情,并希望有一个很好的临时理由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM