Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
类继承Javascript OOP的正确使用_Javascript_Oop_Inheritance - Fatal编程技术网

类继承Javascript OOP的正确使用

类继承Javascript OOP的正确使用,javascript,oop,inheritance,Javascript,Oop,Inheritance,我的语法似乎有些问题,但我不知道是什么问题 我正在使用原型继承(如果我没有弄错的话) 程序应如下所示: 该类必须继承父类的特性,因此当我调用该函数时,它将显示一些文本 下面是我的代码的样子: function myFunction() { var Property = function(dom,height,width){ this.dom = document.createElement("img"); this.height = height;

我的语法似乎有些问题,但我不知道是什么问题

我正在使用原型继承(如果我没有弄错的话)

程序应如下所示:

  • 该类必须继承父类的特性,因此当我调用该函数时,它将显示一些文本
下面是我的代码的样子:

function myFunction() {

    var Property = function(dom,height,width){

        this.dom = document.createElement("img");
        this.height = height;
        this.width = width;

        };

    Property.prototype.display = function() {

    text("The image has: ", this.height+200, this.width+200);
    };

    Image.prototype = Object.create(Property.prototype)

    var firstImage = function(dom,height,width){
        Property.call(this, dom, height,width);
    };

    var image1 = new Image("image.jpg", 10 , 10);
    image1.display();
}
(我认为naomik的方法更好,但如果你只是想弄清楚发生了什么,请看以下内容:)

显然,
Image
不允许扩展(至少在我测试的Firefox中是这样),因为它可以与其他类一起工作

如果,在这一行之后

Image.prototype = Object.create(Property.prototype)
…您可以添加:

alert(Image.prototype.display)
…您将看到它未定义,而如果您添加:

function MyImage () {}
MyImage.prototype = Object.create(Property.prototype)
alert(MyImage.prototype.display); // Alerts the "display" function

我想那是因为你不能替换整个原型。尝试添加到原型中(尽管这对构造函数没有帮助)。

您没有
图像
构造函数。。。