简体   繁体   English

尝试理解Java中的这段代码

[英]Trying to understand this code in Javascript

// Sim.App - application class (singleton)

    Sim.App = function()
    {   
        this.renderer = null;
        this.scene = null;
        this.camera = null;
        this.objects = [];
    }

    // Constructor
    EarthApp = function()
    {
        Sim.App.call(this);
    }

// Subclass Sim.App
EarthApp.prototype = new Sim.App();

============================================= =============================================

In the above, I don't understand why the author used this statement 在上面,我不明白为什么作者使用了这种说法

EarthApp.prototype = new Sim.App();

He could have used 他本可以使用

EarthApp = new Sim.App();

Please help me understand the use of 'prototype' within that statement . 请帮助我理解该声明中“原型”的用法。

The line 线

EarthApp.prototype = new Sim.App();

...creates a Sim.App object and assigns it to the prototype property of the EarthApp function. ...创建一个Sim.App对象,并将其分配给EarthApp函数的prototype属性。 That means that when we do this: 这意味着当我们这样做时:

var e = new EarthApp();

...the e object will get the object from EarthApp.prototype as its prototype, giving it access to the properties and methods of that object. ... e对象将从EarthApp.prototype获得该对象作为其原型,从而可以访问该对象的属性和方法。


FWIW, the inheritance implemented by that code isn't really ideal, because it's calling the Sim.App constructor to create the EarthApp.prototype object, then calling it again to initialize instances. FWIW,由该代码实现的继承并不是真正理想的,因为它正在调用Sim.App构造函数来创建EarthApp.prototype对象,然后再次调用它以初始化实例。 If you want to chain constructors together in that way, here's the more correct way to do it: 如果您想通过这种方式将构造函数链接在一起,这是更正确的方法:

// Reusable `derive` function
// Set up `child`'s `prototype` property to be based on `parent`'s `prototype` property
function derive(child, parent)
{
    function ctor()
    {
    }

    ctor.prototype = parent.prototype;
    child.prototype = new ctor();
    child.prototype.constructor = parent;
}

// Sim.App - application class (singleton)
Sim.App = function()
{   
    this.renderer = null;
    this.scene = null;
    this.camera = null;
    this.objects = [];
};

// Constructor
EarthApp = function()
{
    Sim.App.call(this);
};

// Subclass Sim.App
derive(EarthApp, Sim.App);

You might want to check out my Lineage helper script, which does the above and handles all the plumbing required to handle "supercalls" correctly and such. 您可能想要查看我的Lineage助手脚本,该脚本可以完成上述任务,并处理正确处理“超级调用”所需的所有管道。

It's also worth noting that this is just one way to use JavaScript's prototypical inheritance, which is tremendously flexible. 还值得注意的是,这只是使用JavaScript原型继承的一种方法,它具有极大的灵活性。

Prototypes are a fundamental piece in Javascript's inheritance model. 原型是Java继承模型的基本组成部分。 I recommend you read about it, because without understading it you'll not "get" JS fully. 我建议您阅读有关它的内容,因为如果您不懂它,就不会完全“获得” JS。

Having that said, assinging an object as a function's prototype makes this object be in a prototype chain of every instance of this function created afterwards. 话虽如此,将一个对象作为函数的原型来使该对象成为此函数随后创建的每个实例的原型链。

The way prototype chain works is (more or less) (see example below): 原型链的工作方式(或多或少)(请参见下面的示例):

  1. You try to access "foo" variable in an object 您尝试访问对象中的“ foo”变量
  2. If this object have "foo", return it's value. 如果此对象具有“ foo”,则返回它的值。
  3. If this object does not have "foo", look at it's prototype ('Sim.App' instance in your case) - does it have "foo"? 如果此对象没有“ foo”,请查看它的原型(在您的情况下为“ Sim.App”实例)-它是否具有“ foo”? If so, return it's value. 如果是这样,则返回其值。
  4. If object's prototype does not have a "foo", look at prototype's prototype - and so on, up through the chain. 如果对象的原型没有“ foo”,请查看原型的原型-依次类推。

If you want to read more on that, have a look at this article . 如果您想了解更多信息,请阅读这篇文章

Example - consider: 示例-考虑:

var Sim = {};
Sim.App = function () {
    this.foo = 'sim';
}

EarthApp = function () {
}

EarthApp.prototype = new Sim.App();

var earth = new EarthApp();

// I can access earth.foo even though 'earth' is an instance of 'EarthApp' 
// and not 'Sim.App'. That's because instance of 'Sim.App' is in 
// earth's prototype chain.
console.log(earth.foo);

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

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