如何附加到从闭包创建的JavaScript命名空间?

如何附加到从闭包创建的JavaScript命名空间?,javascript,namespaces,javascript-namespaces,Javascript,Namespaces,Javascript Namespaces,我有一个允许公共和私有成员使用的以下格式的命名空间: function A() { return('a'); } namespace1 = (function () { // private namespace2 = (function() { // private prC = function () { return(namespace1.puB() + 'c'); }; puC

我有一个允许公共和私有成员使用的以下格式的命名空间:

function A() {
    return('a');
}

namespace1 = (function () {
    // private
    namespace2 = (function() {
        // private
        prC = function () {
            return(namespace1.puB() + 'c');
        };
        puC = function () {
            return(prC());
        };
        // public
        return({
            puC: puC
        });
    })();
    prB = function () {
        return(A() + 'b');
    };
    puB = function () {
        return(prB());
    };
    // public
    return({
        puB: puB,
        namespace2: namespace2
    });
})();

document.write('A() = '); try {  document.write(A()); } catch (ex) { document.write('inaccessible'); }
document.write('<BR />');
document.write('namespace1.prB() = '); try {  document.write(namespace1.prB()); } catch (ex) { document.write('inaccessible'); }
document.write('<BR />');
document.write('namespace1.puB() = '); try {  document.write(namespace1.puB()); } catch (ex) { document.write('inaccessible'); }
document.write('<BR />');
document.write('namespace1.namespace2.prC() = '); try {  document.write(namespace1.namespace2.prC()); } catch (ex) { document.write('inaccessible'); }
document.write('<BR />');
document.write('namespace1.namespace2.puC() = '); try {  document.write(namespace1.namespace2.puC()); } catch (ex) { document.write('inaccessible'); }
如何将公共和私有成员添加到这样的名称空间(即:来自不同的文件)

“如何将公共和私有成员附加到这样一个命名空间…”

您的函数是公开的,因为您没有使用
var
正确声明变量

但一旦修复了这个问题,就可以从任何可以引用
名称空间
对象的代码中添加更多的公开属性(因为所有属性都是公开的)


添加更多引用局部(专用术语)函数的属性,您需要一个新函数和变量范围

只需调用引用
名称空间
对象的函数,在该函数中创建一些函数,并添加引用这些本地函数的属性

// Other file

(function() {
    var newLocalFunc = function() {
        // local function
    }

    var anotherLocalFunc = function() {
        // local function
    }

    namespace1.exposedFunc = function() {
        return newLocalFunc()
    }
    namespace1.namespace2.anotherExposedFunc = function() {
        return anotherLocalFunc()
    }
})();
再一次。。。不要忘记在原始代码中的变量前面加上
var

“如何将公共和私有成员附加到这样一个命名空间…”

您的函数是公开的,因为您没有使用
var
正确声明变量

但一旦修复了这个问题,就可以从任何可以引用
名称空间
对象的代码中添加更多的公开属性(因为所有属性都是公开的)


添加更多引用局部(专用术语)函数的属性,您需要一个新函数和变量范围

只需调用引用
名称空间
对象的函数,在该函数中创建一些函数,并添加引用这些本地函数的属性

// Other file

(function() {
    var newLocalFunc = function() {
        // local function
    }

    var anotherLocalFunc = function() {
        // local function
    }

    namespace1.exposedFunc = function() {
        return newLocalFunc()
    }
    namespace1.namespace2.anotherExposedFunc = function() {
        return anotherLocalFunc()
    }
})();

再一次。。。不要忘记将
var
放在原始代码中变量的前面。

任何未声明
var
关键字的变量都将在全局范围内。因此,您的
puB()
函数不是不可访问或私有的,它只是不是
namespace1
函数返回的对象的成员。尝试
window.prB()
例如,您将看到该方法存在于
window
对象的全局范围内

<head>
    <script type="text/javascript">
        obj1 = {}; //in global scope
        var obj2 = {}; //in global scope. Although used the var keyword, this line itself is in the global scope; so the variable.
        function someFunc() {
            obj3 = {}; //in global scope
            var obj4 = {}; //'so-called' private (inaccessible from global scope)
        }
    </script>
</head>
文件-2.js

var namespace1 = (function() {
    // some code...
    var namespace2 = (function() {
        // some code...
        return {
            obj2: 'value2'
        };
    })();

    return {
        obj1: 'value1'
    };
})();
namespace1.namespace3 = (function() {
    // some code...
    var ns4 = (function() {
        // some code...
        return {
            obj4: 'value4'
        };
    })();

    return {
        obj3: 'value3',
        namespace4: ns4
    };
})();
什么是什么:

  • namespace1
    在全局范围内声明;因此,它是可访问的 这是我们的主要目标
  • namespace2
    不可访问(私有)
  • namespace3
    在全局范围内不可访问,但可以作为
    名称空间1
    的成员;e、 g.:
    namespace1.namespace3
  • namespace4
    可以作为
    namespace1
    的成员访问。例如。:
    namespace1.namespace4
所以,;我们的主要对象名称空间1的成员是:

namespace1 = {
    obj1: String,
    namespace3: {
        obj3: String,
        namespace4: {
            obj4: String
        }
    }
};

未使用
var
关键字声明的任何变量都将在全局范围内。因此,您的
puB()
函数不是不可访问或私有的,它只是不是
namespace1
函数返回的对象的成员。尝试
window.prB()
例如,您将看到该方法存在于
window
对象的全局范围内

<head>
    <script type="text/javascript">
        obj1 = {}; //in global scope
        var obj2 = {}; //in global scope. Although used the var keyword, this line itself is in the global scope; so the variable.
        function someFunc() {
            obj3 = {}; //in global scope
            var obj4 = {}; //'so-called' private (inaccessible from global scope)
        }
    </script>
</head>
文件-2.js

var namespace1 = (function() {
    // some code...
    var namespace2 = (function() {
        // some code...
        return {
            obj2: 'value2'
        };
    })();

    return {
        obj1: 'value1'
    };
})();
namespace1.namespace3 = (function() {
    // some code...
    var ns4 = (function() {
        // some code...
        return {
            obj4: 'value4'
        };
    })();

    return {
        obj3: 'value3',
        namespace4: ns4
    };
})();
什么是什么:

  • namespace1
    在全局范围内声明;因此,它是可访问的 这是我们的主要目标
  • namespace2
    不可访问(私有)
  • namespace3
    在全局范围内不可访问,但可以作为
    名称空间1
    的成员;e、 g.:
    namespace1.namespace3
  • namespace4
    可以作为
    namespace1
    的成员访问。例如。:
    namespace1.namespace4
所以,;我们的主要对象名称空间1的成员是:

namespace1 = {
    obj1: String,
    namespace3: {
        obj3: String,
        namespace4: {
            obj4: String
        }
    }
};

你离堪萨斯州很远

您不能将事物“声明”为公共或私人。
只要您在函数内部定义对象,然后选择返回特定的对象,或者将它们附加到作为参数传入的对象/数组中,那么在函数返回后,您将拥有对这些对象的“公共”(外部)访问权

为了拥有“私有”访问权,您返回一个引用内部内容的函数

var Wallet = function (amount, overdraft_limit) {
    var balance = 0,
        overdraft = overdraft_limit || 0,

        deposit_funds = function (funds) { balance += funds; return true; },

        withdraw_funds  = function (request) {
            var funds = 0;
            balance -= request;
            funds = request;
            return funds;
        },

        validate_request = function (pin) { /* ... */ },

        sufficient_funds = function (val) { return val <= (balance + overdraft); },

        add = function (pin, deposit) {
            if (!validate_request(pin) || deposit <= 0) { return false; }

            var result = deposit_funds(deposit);
            return result;
        },

        deduct = function (pin, withdrawl) {
            if (!validate_request(pin) || withdrawl <= 0) { return false; }
            if (!sufficient_funds(withdrawl)) { return false; }

            var funds = withdraw_funds(withdrawl);
            return funds;
        },

        check = function () { return balance; },

        public_interface = { deduct : deduct, add : add, check : check };

    return public_interface;
};


var myWallet = Wallet(30, 20);

var cash = myWallet.deduct(40);
cash;             //  40
myWallet.check(); // -10

myWallet.balance = 40000000000;

cash = myWallet.deduct(4000);
cash;  // === false
var Wallet=功能(金额、透支限额){
var余额=0,
透支=透支限额| | 0,
存款资金=函数(资金){余额+=资金;返回true;},
提取资金=功能(请求){
var基金=0;
余额-=请求;
资金=请求;
返还资金;
},
验证请求=函数(pin){/*…*/},

充足的资金=功能(val){return val这里离堪萨斯州很远

您不能将事物“声明”为公共或私人。
只要您在函数内部定义对象,然后选择返回特定的对象,或者将它们附加到作为参数传入的对象/数组中,那么在函数返回后,您将拥有对这些对象的“公共”(外部)访问权

为了拥有“私有”访问权,您返回一个引用内部内容的函数

var Wallet = function (amount, overdraft_limit) {
    var balance = 0,
        overdraft = overdraft_limit || 0,

        deposit_funds = function (funds) { balance += funds; return true; },

        withdraw_funds  = function (request) {
            var funds = 0;
            balance -= request;
            funds = request;
            return funds;
        },

        validate_request = function (pin) { /* ... */ },

        sufficient_funds = function (val) { return val <= (balance + overdraft); },

        add = function (pin, deposit) {
            if (!validate_request(pin) || deposit <= 0) { return false; }

            var result = deposit_funds(deposit);
            return result;
        },

        deduct = function (pin, withdrawl) {
            if (!validate_request(pin) || withdrawl <= 0) { return false; }
            if (!sufficient_funds(withdrawl)) { return false; }

            var funds = withdraw_funds(withdrawl);
            return funds;
        },

        check = function () { return balance; },

        public_interface = { deduct : deduct, add : add, check : check };

    return public_interface;
};


var myWallet = Wallet(30, 20);

var cash = myWallet.deduct(40);
cash;             //  40
myWallet.check(); // -10

myWallet.balance = 40000000000;

cash = myWallet.deduct(4000);
cash;  // === false
var Wallet=功能(金额、透支限额){
var余额=0,
透支=透支限额| | 0,
存款资金=函数(资金){余额+=资金;返回true;},
提取资金=功能(请求){
var基金=0;
余额-=请求;
资金=请求;
返还资金;
},
验证请求=函数(pin){/*…*/},

充足的_funds=function(val){return val这只是一堆返回对象的自参与函数,问题是返回的