javascript 单例模式 - 创建不同实例的问题

发布于 2024-11-25 05:36:41 字数 2946 浏览 2 评论 0原文

我在从我编写的 javascript 类创建实例/对象时遇到问题。您能否检查代码并查看代码底部的输出。

提前致谢... 问候。

//MARKER MANAGER CLASS
            var MarkerManager = (function(){

                var arr = [];
                var arrName = [];
                var counter = 0;

                var MarkerManager = function (args){

                    arr = [];
                    arrName = [];
                    counter = 0;            

                    return {
                        addMarker: addMarker,
                        getMarker: getMarker,
                        getIndex: getIndex
                    }

                };

                function getIndex(){
                    return counter;
                }

                function addMarker(marker){
                    arrName[counter] = marker.name;
                    arr[counter] = marker;
                    counter++;
                }

                function getMarkerNameList(){
                    return arrName;
                }

                function getMarker(li_name){
                    var markerIndex = arrName.indexOf(li_name);         
                    return  arr[markerIndex];
                }



                /*var instance;

                var _static = {

                    name: 'manager',

                    getInstance: function (args){

                        if(instance === undefined){
                            instance = new MarkerManager(args)
                        }

                        return instance;

                    }               

                };

                return _static;
                */

                return function (args){

                    var manager = new MarkerManager(args);
                    return manager;

                }

            })();


            var marker1 = new Marker({name:'test0', lat:3 ,  lng:5, isDraggable:true, iconType:'red' });
            var marker2 = new Marker({name:'test1', lat:33 , lng:55, isDraggable:true, iconType:'red' });
            var marker3 = new Marker({name:'test2', lat:44 , lng:99, isDraggable:true, iconType:'red' });
            var marker4 = new Marker({name:'test3', lat:55 , lng:77, isDraggable:true, iconType:'red' });
            var marker5 = new Marker({name:'test4', lat:88 , lng:65, isDraggable:true, iconType:'red' });

            var manager  = new MarkerManager({test:'test2'}); //MarkerManager.getInstance({map:'testtt'});//
            var manager2 = new MarkerManager({test:'test1'});

            manager.addMarker(marker1);
            manager.addMarker(marker2);
            manager.addMarker(marker3);

            manager2.addMarker(marker4);
            manager2.addMarker(marker5);

            alert(manager.getMarkerNameList());     
            alert(manager2.getMarkerNameList());

        //output is "test0,test1,test2,test3,test4" for both manager objects. How can I separate them 

I have a problem in creating instances/objects from a javascript class that I have written. Can you please check the code and see the output which is at the bottom of the code.

Thanks in advance...
Regards.

//MARKER MANAGER CLASS
            var MarkerManager = (function(){

                var arr = [];
                var arrName = [];
                var counter = 0;

                var MarkerManager = function (args){

                    arr = [];
                    arrName = [];
                    counter = 0;            

                    return {
                        addMarker: addMarker,
                        getMarker: getMarker,
                        getIndex: getIndex
                    }

                };

                function getIndex(){
                    return counter;
                }

                function addMarker(marker){
                    arrName[counter] = marker.name;
                    arr[counter] = marker;
                    counter++;
                }

                function getMarkerNameList(){
                    return arrName;
                }

                function getMarker(li_name){
                    var markerIndex = arrName.indexOf(li_name);         
                    return  arr[markerIndex];
                }



                /*var instance;

                var _static = {

                    name: 'manager',

                    getInstance: function (args){

                        if(instance === undefined){
                            instance = new MarkerManager(args)
                        }

                        return instance;

                    }               

                };

                return _static;
                */

                return function (args){

                    var manager = new MarkerManager(args);
                    return manager;

                }

            })();


            var marker1 = new Marker({name:'test0', lat:3 ,  lng:5, isDraggable:true, iconType:'red' });
            var marker2 = new Marker({name:'test1', lat:33 , lng:55, isDraggable:true, iconType:'red' });
            var marker3 = new Marker({name:'test2', lat:44 , lng:99, isDraggable:true, iconType:'red' });
            var marker4 = new Marker({name:'test3', lat:55 , lng:77, isDraggable:true, iconType:'red' });
            var marker5 = new Marker({name:'test4', lat:88 , lng:65, isDraggable:true, iconType:'red' });

            var manager  = new MarkerManager({test:'test2'}); //MarkerManager.getInstance({map:'testtt'});//
            var manager2 = new MarkerManager({test:'test1'});

            manager.addMarker(marker1);
            manager.addMarker(marker2);
            manager.addMarker(marker3);

            manager2.addMarker(marker4);
            manager2.addMarker(marker5);

            alert(manager.getMarkerNameList());     
            alert(manager2.getMarkerNameList());

        //output is "test0,test1,test2,test3,test4" for both manager objects. How can I separate them 

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

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

发布评论

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

评论(1

半夏半凉 2024-12-02 05:36:41

您需要移动所有变量 &管理器所需的函数到构造函数。或者 MarkerManager 将共享相同的变量和变量。一起发挥作用。

        var MarkerManager = (function(){
            return function (args){

                var arr = [];
                var arrName = [];
                var counter = 0;

                var MarkerManager = function (args){

                    arr = [];
                    arrName = [];
                    counter = 0;            

                    return {
                        addMarker: addMarker,
                        getMarker: getMarker,
                        getIndex: getIndex
                    }

                };

                function getIndex(){
                    return counter;
                }

                function addMarker(marker){
                    arrName[counter] = marker.name;
                    arr[counter] = marker;
                    counter++;
                }

                function getMarkerNameList(){
                    return arrName;
                }

                function getMarker(li_name){
                    var markerIndex = arrName.indexOf(li_name);         
                    return  arr[markerIndex];
                }


                var manager = new MarkerManager(args);
                return manager;

            }

        })();

You need to move all the variables & functions needed by the manager to the constructor function. Or the MarkerManagers will share the same variables & functions together.

        var MarkerManager = (function(){
            return function (args){

                var arr = [];
                var arrName = [];
                var counter = 0;

                var MarkerManager = function (args){

                    arr = [];
                    arrName = [];
                    counter = 0;            

                    return {
                        addMarker: addMarker,
                        getMarker: getMarker,
                        getIndex: getIndex
                    }

                };

                function getIndex(){
                    return counter;
                }

                function addMarker(marker){
                    arrName[counter] = marker.name;
                    arr[counter] = marker;
                    counter++;
                }

                function getMarkerNameList(){
                    return arrName;
                }

                function getMarker(li_name){
                    var markerIndex = arrName.indexOf(li_name);         
                    return  arr[markerIndex];
                }


                var manager = new MarkerManager(args);
                return manager;

            }

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