0

说我有这样的东西

define(["jquery"], function($) {        
    return function() {
        //public members(use this)
        this.publicMember = "I'm a public member";

        //private members (use var)
        var privateMember = "I'm a priavte member";


        //private method
        function privateMethod() {
            console.log('private function said: ' + privateMember);
        };

        //privileged method
        //can access private variables
        this.privilegedMethod = function() {
            console.log('privileged function said: ' + this.publicMember + ' and ' + privateMember );
        };


    };
});

在返回调用者并确保我没有向全局范围公开任何内容之前,如何添加公共方法?

???????.prototype.publicMethod= function () {
    console.log('public function said: ' + this.publicMember);
}
4

1 回答 1

1
define(["jquery"], function($) {        
    var className = function() {
        //public members(use this)
        this.publicMember = "I'm a public member";

        //private members (use var)
        var privateMember = "I'm a priavte member";


        //private method
        function privateMethod() {
            console.log('private function said: ' + privateMember);
        };

        //privileged method
        //can access private variables
        this.privilegedMethod = function() {
            console.log('privileged function said: ' + this.publicMember + ' and ' + privateMember );
        };


    };
    className.prototype.publicMethod = function() {};
    return className;
});
于 2013-07-07T01:35:09.387 回答