简体   繁体   English

如何在Javascript中实现像“var obj = new OpenLayers.Layer.WMS(... ...)”这样的继承

[英]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: 在OpenLayers中,我们可以使用以下语法创建对象:

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: 整个事情可以在一个javascript文字中定义,如下所示:

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. “命名空间对象”只是用于普通javascript对象的这种使用的术语。

This has nothing to do with inheritance or the prototype mechanism - just plain javascript objects. 这与继承或原型机制无关 - 只是简单的javascript对象。

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

var obj = Object.create(Object name);

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

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