简体   繁体   English

在javascript对象文字中获取set访问权限

[英]get set access in javascript object literals

Using object literals for the first time and it seems I don't quite understand them right. 第一次使用对象文字,似乎我不太了解它们。 What I need is private variables with the scope of the object and therefore accessible to all functions in the object literal. 我需要的是具有对象范围的私有变量,因此可以访问对象文字中的所有函数。

Here's the issue: 这是问题所在:

I have a simple object literal for a map object 我有一个简单的对象文字用于地图对象

var mapObj = {

    init: function (lat, lng, documentID) {
        var myOptions = {
            center: new google.maps.LatLng(lat, lng),
            zoom: 18,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById(documentID),
                myOptions);

        return map;
    },

    setMarker: function (lat, lng, map, contentObj) {
        var myLatlng = new google.maps.LatLng(lat, lng);
        var marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            title: "Hello World!",
            content: contentObj
        });
    }
};

This is all fine, and I get to use it as mapObj.init(....) and mapObj.setMarker(...) which work perfectly. 这一切都很好,我可以使用它作为mapObj.init(....)和mapObj.setMarker(...)完美地工作。

What I want to be able to do is store some of these variable values - like 'map' in the init() method, and then have it accessible for other methods. 我想要做的是存储一些这些变量值 - 比如init()方法中的'map',然后让其他方法可以访问它们。 For eg I should not need to pass 'map' in the setMarker, as the mapObj should hold the map internally after the init method. 例如,我不需要在setMarker中传递'map',因为mapObj应该在init方法之后内部保存map。

Similarly I'd like to be able to do mapObj.getMap() to access the map object created in init. 类似地,我希望能够使用mapObj.getMap()来访问在init中创建的地图对象。

I'm unable to figure out how it works. 我无法弄清楚它是如何工作的。 Declaring var map as 将var map声明为

var mapObj {
   var map
}

for eg throws errors. 例如,抛出错误。

Am I expecting this to work too much like C#? 我是否希望这样做太像C#一样? Should I be using the classic javascript 'class' constructions? 我应该使用经典的javascript'class'结构吗? Any pointers will help so I can move on. 任何指针都会有所帮助,所以我可以继续前进。

Thanks 谢谢

Just wrap it in a function like so: 只需将其包装在如下函数中:

var mapObj;
!function() {

    var map;

    mapObj = {

        init: function (lat, lng, documentID) {
            var myOptions = {
                center: new google.maps.LatLng(lat, lng),
                zoom: 18,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map(document.getElementById(documentID),
                    myOptions);

            return map;
        },

        setMarker: function (lat, lng, map, contentObj) {
            var myLatlng = new google.maps.LatLng(lat, lng);
            var marker = new google.maps.Marker({
                position: myLatlng,
                map: map,
                title: "Hello World!",
                content: contentObj
            });
        }
    };
}();

Now map is only available inside that function (and all functions it contains). 现在map只在该函数内部(以及它包含的所有函数)中可用。 mapObj is available outside. mapObj在外面可用。

Closures reporting in 关闭报告

var mapObj = (function() {

    var map; //This is private

    var publics = {
        init: function(lat, lng, documentID) {
            var myOptions = {
                center: new google.maps.LatLng(lat, lng),
                zoom: 18,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map(document.getElementById(documentID), myOptions);
            return map;
        },

        setMarker: function(lat, lng, map, contentObj) {
            var myLatlng = new google.maps.LatLng(lat, lng);
            var marker = new google.maps.Marker({
                position: myLatlng,
                map: map,
                title: "Hello World!",
                content: contentObj
            });
        }
    };
    return publics;

})(window);

Everything inside publics is available outside, everything else is private, and shared. 公共场所内的一切都在外面,其他一切都是私密的,并且是共享的。

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

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