Javascript中的继承 [英] Inheritence in Javascript

查看:161
本文介绍了Javascript中的继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用原型在JavaScript中实现inheritence时,我得到了一个奇怪的错误。我想知道是否有人可以解释这一点。在下面的代码中,
我试图从父类派生一个子类:
$ b $ pre $ parent $ class = function如果(!parent_class.prototype._vtbl)
{
parent_class.prototype.parent_func = function(o){return alert(parent_func);

) }
parent_class.prototype._vtbl = true;




$ b child = function(byref)
{
parent_class.call(this,byref);

if(!child.prototype._vtbl)
{
child.prototype = new parent_class;
child.prototype.child_func = parent_class.prototype.parent_func;

child.prototype._vtbl = true;




$ b函数dotest()
{
var pub = new child;
alert(pub.child_func);

var pub2 =新的孩子;
alert(pub2.child_func);
}

dotest();

在浏览器(Firefox或IE)中运行测试时,会得到两个警报。第一个说pub.child_func是未定义的,第二个说pub.child_func是一个有效的函数,是parent_class.parent_func。你为什么看到这种行为。这是一个错误?

解决方案

这样的构造的JavaScript执行顺序:

 函数SomeClass(){body(); } 
var x = new SomeClass();

是这样的:


  1. 继承自 SomeClass.prototype 的新对象被创建(在构造函数的代码被执行之前,这里选择对象的原型)$
  2. body(); 得到执行

  3. 创建的对象被分配给x

在你的例子中你可以做的是使用 .__ proto __ ,尽管你真的不应该这样做:

  child = function(byref){
parent_class.call(this,byref);
if(!child.prototype._vtbl){
child.prototype = new parent_class;
child.prototype.child_func = parent_class.prototype.parent_func;
child.prototype._vtbl = true;
}
this .__ proto__ = child.prototype;

$ / code>

你应该做的是:

  child = function(byref){
parent_class.call(this,byref);
}
child.prototype = Object.create(parent_class.prototype);
child.prototype.child_func = parent_class.prototype.parent_func;
child.prototype._vtbl = true;


I get a strange bug when I implement inheritence in Javascript using prototypes. I am wondering if someone can explain this. In the following code, I am trying to derive a child class from a parent class:

            parent_class=function(byref)
            {   
                if( !parent_class.prototype._vtbl )
                {
                        parent_class.prototype.parent_func= function(o) { return alert("parent_func"); }
                        parent_class.prototype._vtbl = true;
                }
            }



            child=function(byref)
            {
                parent_class.call(this,byref);

                if( !child.prototype._vtbl )
                {
                        child.prototype = new parent_class;
                        child.prototype.child_func      = parent_class.prototype.parent_func;

                        child.prototype._vtbl = true;
                }

            }


            function dotest()
            {
                var pub = new child;
                alert( pub.child_func );

                var pub2    = new child;
                alert( pub2.child_func );
            }

            dotest();

When you run the test in a browser (Firefox or IE), you get two alerts. The first one says that pub.child_func is undefined, the second one says that the pub.child_func is a valid function and is parent_class.parent_func. Why do you see this behavior. Is this a bug?

解决方案

Order of execution in javascript of such construct:

function SomeClass () { body(); }
var x = new SomeClass();

is this:

  1. new object which inherits from SomeClass.prototype is created (the prototype for the object is chosen here, before code of the constructor is executed)
  2. body(); gets executed
  3. created object is assigned to x

What you can do in your example is use .__proto__, although you really really should not:

child = function (byref) {
    parent_class.call(this, byref);
    if (!child.prototype._vtbl) {
        child.prototype = new parent_class;
        child.prototype.child_func = parent_class.prototype.parent_func;
        child.prototype._vtbl = true;
    }
    this.__proto__ = child.prototype;
}

What you really should do is this:

child = function (byref) {
    parent_class.call(this, byref);
}
child.prototype = Object.create(parent_class.prototype);
child.prototype.child_func = parent_class.prototype.parent_func;
child.prototype._vtbl = true;

这篇关于Javascript中的继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆