這裡要來介紹 leaflet.js 提供什麼樣的特性能夠開發套件
建立類別資料
const MyDemoClass = L.Class.extend({
myDemoProperty: 42,
});
const myDemoInstance = new MyDemoClass();
console.log(myDemoInstance.myDemoProperty);
將以建立好的類別擴充資料
新增屬性 newProperty
const MyDemoClass = L.Class.extend({
myDemoProperty: 42,
});
MyDemoClass.include({
newProperty: 78,
});
const mySecondDemoInstance = new MyDemoClass();
console.log(mySecondDemoInstance.newProperty);
如果要在初始化修改已建立的類別可用 L.setOptions() 修改
const MyBoxClass = L.Class.extend({
options: {
width: 1,
height: 1,
},
initialize: function (name, options) {
this.name = name;
L.setOptions(this, options);
},
});
var instance = new MyBoxClass("Red", { width: 10 });
console.log(instance.name);
console.log(instance.options.width);
console.log(instance.options.height);
https://leafletjs.com/examples/extending/extending-1-classes.html