问题描述
我正在查看扩展传单文档,以添加自定义控件.
I am looking at the Extending Leaflet documentation for adding custom controls.
其中包含以下代码段,作为添加简单水印控件的示例:
It contains this code snippet as an example of adding a simple watermark control:
L.Control.Watermark = L.Control.extend({
onAdd: function(map) {
var img = L.DomUtil.create('img');
img.src = '../../docs/images/logo.png';
img.style.width = '200px';
return img;
},
onRemove: function(map) {
// Nothing to do here
}
});
L.control.watermark = function(opts) {
return new L.Control.Watermark(opts);
}
L.control.watermark({ position: 'bottomleft' }).addTo(map);
为什么将控件同时分配给大写(L.Control.Watermark
)和小写L.control.watermark
变量?扩展JavaScript库时,这是常见的约定吗?
Why is the control assigned to both uppercase (L.Control.Watermark
) and lowercase L.control.watermark
variables? Is this a common convention when extending JavaScript libraries?
推荐答案
If you look at the tutorial named Extending Leaflet: Class Theory, you'll see a section named Factories:
基本上, lowerCamelCase 函数是实例化相应类的一种便捷方法.您会发现很多文章,为什么要在构造函数上使用工厂函数,这个似乎很全面
Basically, the lowerCamelCase function is a convenience method to instantiate the corresponding class. You'll find plenty of articles on why use factory functions over constructors, this one seems quite comprehensive
这篇关于为什么将自定义传单控件添加为大写和小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!