简体   繁体   中英

How to implement inheritance like “var obj = new OpenLayers.Layer.WMS(… …)” in Javascript

In OpenLayers, we can create a object by using the following syntax:

var obj = new OpenLayers.Layer.WMS(... ...);

How to define that inheritance? I know the prototype mechanism but I have no idea about how to implement this inheritance.

You define the each successive object like this:

var OpenLayers = {};
OpenLayers.Layer = {};
OpenLayers.Layer.WMS = function() {}

The whole thing could be defined in one javascript literal like this:

var OpenLayers = {
    Layer: {
        WMS: function() {}
    }
}

These are generally called namespace objects which are just containers for organizing groups of functions in a logical manner outside of the global namespace. A "namespace object" is just a term used for this type of usage of a normal javascript object.

This has nothing to do with inheritance or the prototype mechanism - just plain javascript objects.

我们可以实现这样的继承:

var obj = Object.create(Object name);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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