我正在查看扩展传单文档以添加 custom controls .
它包含此代码片段作为添加简单水印控件的示例:
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 库时的常见约定吗?
最佳答案
如果您查看名为 Extending Leaflet: Class Theory 的教程,您会看到一个名为 Factories 的部分:
Most Leaflet classes have a corresponding factory function. A factory function has the same name as the class, but in lowerCamelCase instead of UpperCamelCase.
基本上,lowerCamelCase 函数是实例化相应类的便捷方法。您会找到很多关于为什么使用工厂函数而不是构造函数的文章,this one看起来很全面
关于javascript - 为什么自定义传单控件添加为大写和小写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51657310/