みんな!サブクラスからスーパークラスのインスタンス変数にアクセスするには?
私はJavaScriptでこのクラスがあるとします。
function Animal()
{
this.name = "name";
}
Animal.prototype.someMethod =
function()
{
}
とこのサブクラス:
猫でオーバーライドされたメソッドからスーパークラス「名前」インスタンス変数にアクセスするための構文は何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
}
クラス?
ありがとうございます。
マルコス
UPDATED:あなたは実際のコードを見たい場合はまあ、ここにあります。問題は、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;
ワンノート::; ''あなたのCat'コンストラクタで、本当に何もしないライン 'Animal.call(本)あなたの例にこれを適用する
はこのような何かを作り出します。これはJavaではありません。 「スーパー」に類似するものは何もない。 –
@lwburk - この記事は反対です:[記事](https://developer.mozilla.org/en/Introduction_to_Object-Oriented_JavaScript) – Marcos
本当のスーパーは魔法の "this"変数のように動作し、誰がスーパークラスitsefです。スーパー機能をエミュレートするJavascriptでは、スーパークラスからメソッドを「統計的にディスパッチする」必要があります。 btw、あなたは私たちが見ることができるものから間違って何もしていないので、JSFiddleで実行中の例を取得しようとする必要があります。 – hugomg