简体   繁体   English

如何从子类访问超类实例变量?

[英]How to access a superclass instance variable from the subclass?

everybody! 大家!

Suppose that I have this class in JavaScript: 假设我在JavaScript中有此类:

function Animal()
{
    this.name = "name";
}

Animal.prototype.someMethod =
    function ()
    {
    }

and this subclass: 和这个子类:

function Cat()
{
    Animal.call(this);
}

Cat.prototype = new Animal();
Cat.prototype.constructor = Cat;

Cat.prototype.someMethod =
    function ()
    {
        // I want to access the superclass "name" instance variable here
    }

What's the syntax to access the superclass "name" instance variable from the overriden method in the Cat class? 从Cat类中的重写方法访问超类“名称”实例变量的语法是什么?

Thank you. 谢谢。

Marcos 马可斯

UPDATED : Well, if you want to see the real code, here it is. 更新 :好吧,如果您想查看真实的代码,就在这里。 The problem is with the abc variable (just a test variable that I was using). 问题出在abc变量(只是我正在使用的测试变量)上。

var pesquisaAcervo;

$(
    function ()
    {
        carregadoBase();

        if ($("#form\\:tipoPesquisa").val() == "SIMPLES")
        {
            pesquisaAcervo = new PesquisaAcervoSimples();
        }
        else
        {
            pesquisaAcervo = new PesquisaAcervoAvancada();
        }
        pesquisaAcervo.paginaCarregada();
    }
);

// --- PesquisaAcervo ----------------------------------------------------------

function PesquisaAcervo()
{
    $("*:visible[id^='form:materiaisPesquisa']").
        change(this.materialMudado).keyup(this.materialMudado);

    this.abc = 10;
}

PesquisaAcervo.prototype.paginaCarregada =
    function ()
    {
        $("#cabecalhoPesquisa a").click(this.exibirDicasPesquisa);
        $("#cabecalhoPesquisa select").
            change(function () {$("#form").submit();}).
            keyup(function () {$(this).change();});

        $("*:visible[class*='foco']").focus().select();
    };

PesquisaAcervo.prototype.materialMudado =
    function ()
    {
    };

PesquisaAcervo.prototype.exibirDicasPesquisa =  
    function ()
    {
    };

// --- PesquisaAcervoSimples ---------------------------------------------------

function PesquisaAcervoSimples()
{
    PesquisaAcervo.call(this);

    $("#form\\:campos").change(
        function ()
        {
            $("#textoCampo").text($("#form\\:campos :selected").text() + ":");
        }
     ).keyup(function () {$(this).change();}).change();

    $("#pesquisaSimples a").click(
        function ()
        {
            pesquisaAcervo = new PesquisaAcervoAvancada();

            $("#pesquisaSimples").parent().hide();

            $("#pesquisaAvancada").parent().show();

            $("#form\\:tipoPesquisa").val("AVANCADO");
        }
   );
}

PesquisaAcervoSimples.prototype = new PesquisaAcervo();
PesquisaAcervoSimples.prototype.constructor = PesquisaAcervoSimples;

PesquisaAcervoSimples.prototype.materialMudado =
    function ()
    {
        alert(this.abc); // "undefined" here
    };

// --- PesquisaAcervoAvancada --------------------------------------------------

function PesquisaAcervoAvancada()
{
     PesquisaAcervo.call(this);
}

PesquisaAcervoAvancada.prototype = new PesquisaAcervo();
PesquisaAcervoAvancada.prototype.constructor = PesquisaAcervoAvancada;

Your actual code reveals the problem. 您的实际代码揭示了问题所在。 The issue is with how you're calling materialMudado . 问题在于您如何称呼materialMudado It's being invoked as the callback for an event. 它被作为事件的回调调用。 The keyword this inside the callback will refer to the target of the event (which has no abc property), not to the object that the function "belongs" to. 回调中的关键字this将指向事件的目标(不具有abc属性),而不是函数“所属”的对象。

Here's a simple demonstration: 这是一个简单的演示:

function Test() {};

Test.prototype.callback = function() {
    alert(this);
}

var t = new Test();

$(document).click(t.callback);

Output (after clicking page): 输出(单击页面后):

[object HTMLDocument]

Compare to this: 比较一下:

function Test() {};

Test.prototype.callback = function() {
    alert(this);
}

var t = new Test();

$(document).click(function() {
    t.callback();
});

Output: 输出:

[object Object]

In this second example we close over the variable t , retaining a reference to it. 在第二个示例中,我们关闭变量t ,并保留对其的引用。

Applying this to your example produces something like this: 将其应用到您的示例中将产生如下内容:

function PesquisaAcervo() {
    var that = this;
    var callback = function() {
        that.materialMudado();
    };
    $("*:visible[id^='form:materiaisPesquisa']").
        change(callback).keyup(callback);

    this.abc = 10;
}

this.name should work. this.name应该起作用。 I don't see you overriding the name property in your Cat function so you should be able to just do this.name and the protopical chain will do the work to find the first instance of this property which should be Animal.name . 我看不到您在Cat函数中覆盖了name属性,因此您应该只能执行this.name ,而命题链将完成查找该属性的第一个实例Animal.name

Just use the this keyword: 只需使用this关键字:

function Animal()
{
    this.name = "name";
}

Animal.prototype.someMethod2 =
    function ()
    {
    }


function Cat()
{
    Animal.call(this);
}

Cat.prototype = new Animal();
Cat.prototype.constructor = Cat;

Cat.prototype.someMethod =
    function ()
    {
        alert(this.name);// I want to access the superclass "name" instance variable here
    }
var c = new Cat();
c.someMethod();

Add this code to the bottom, I've just added an alert to your someMethod method... 将此代码添加到底部,我刚刚向您的someMethod方法添加了警报...

In your example, Cat derives everything from Animal, so it has access to the name variable 在您的示例中,Cat继承了Animal的所有内容,因此可以访问name变量

There is no such thing as an override for an instance variable. 没有实例变量的替代。 An instance variable is just a property on the this object. 实例变量只是this对象的属性。 You can read it with: 您可以阅读:

var x = this.name;

or assign to it with: 或分配给它:

this.name = "foo";

this.name will access the name whether you have an instance of an Animal object or an instance of a Cat object. 无论您具有Animal对象的实例还是Cat对象的实例, this.name都将访问​​该名称。

If you want to assign to the name property in the Cat constructor, you can just do so with 如果要在Cat构造函数中分配给name属性,则可以使用

this.name = "Cat";

Once you have a working instance of an object, properties are just properties and there is no distinction for whether a property was created by a superclass or a subclass. 一旦有了对象的工作实例,属性就是属性,就没有区别是由超类还是子类创建的。 They're just properties of the object at that point and you access all of them the same way with the this.propertyName syntax. 它们当时只是对象的属性,您可以使用this.propertyName语法以相同的方式访问所有这些this.propertyName

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

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