Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.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原型问题_Javascript_Inheritance_Prototype - Fatal编程技术网

Javascript原型问题

Javascript原型问题,javascript,inheritance,prototype,Javascript,Inheritance,Prototype,我的情况是,我正在使用Box2DWeb开发一个画布游戏,所以我有一个名为Sprite的类,它将图像绘制到画布上,我现在想创建一个PhysicsSprite类,它继承Sprite的方法和属性 我的精灵类(Sprite.js): 我正在PhysicsSprite(PhysicsSprite.js)中设置原型,如下所示: 请注意警报(this.prototype.Position.X),它确实会提醒我该位置,但是如果我将代码更改为this.Position.X,我会收到一个错误,同样,如果我有Phys

我的情况是,我正在使用Box2DWeb开发一个画布游戏,所以我有一个名为Sprite的类,它将图像绘制到画布上,我现在想创建一个PhysicsSprite类,它继承Sprite的方法和属性

我的精灵类(Sprite.js):

我正在PhysicsSprite(PhysicsSprite.js)中设置原型,如下所示:

请注意警报(this.prototype.Position.X),它确实会提醒我该位置,但是如果我将代码更改为this.Position.X,我会收到一个错误,同样,如果我有PhysicsSprite的实例,我必须显式调用原型

这让我觉得我没有设置对象的原型,只是创建了一个名为
prototype
的属性,并将其设置为一个新的精灵

如果有人能帮助我,并解释我做错了什么,那将不胜感激。 我有诵读困难症,所以我总是拼错变量,这很令人沮丧,所以这是我寻找的第一件事,但在我看来一切都很好。

改变

this.prototype = new Sprite(position, dimension, undefined);

您是对的,在更改之前,您正在将对象的实例指定给“prototype”属性

原型是构造函数的属性,而不是对象实例-即,您可以通过
physicsprite.prototype
访问原型,但不能通过
this.prototype
x.prototype
(其中
x=new physicsprite()

this.prototype = new Sprite(position, dimension, undefined);

您是对的,在更改之前,您正在将对象的实例指定给“prototype”属性


prototype是构造函数的一个属性,而不是一个对象实例——也就是说,您可以通过
PhysicsSprite.prototype
访问prototype,但不能通过
this.prototype
x.prototype
(其中
x=new PhysicsSprite()
)访问prototype,尽管他们经常互相误解,因为他们都有{和}D

一个典型的JavaScript类:

function Sprite(a,b) {
    // These are per instance
    this.a = a;
    this.b = b;
}
Sprite.prototype = {
    // These are per class, all instances share them
    Draw: function (g) {
        // ...
    }
};

。。。JavaScript中的继承非常奇怪。

JavaScript与Java是一种非常不同的鸟,尽管它们经常互相误解,因为它们都共享{和}D

一个典型的JavaScript类:

function Sprite(a,b) {
    // These are per instance
    this.a = a;
    this.b = b;
}
Sprite.prototype = {
    // These are per class, all instances share them
    Draw: function (g) {
        // ...
    }
};
。。。JavaScript中的继承非常奇怪

这让我觉得我没有设置对象的原型,只是创建了一个名为prototype的属性,并将其设置为一个新的Sprite

对。此外,您根本没有使用函数的原型,因为您直接将每个函数分配给实例(
this.Draw=function()…
),而不是原型


在JavaScript中,有一种叫做构造函数的东西。使用
new
运算符调用的每个函数都是构造函数

以这种方式调用函数(
new SomeFunc()
)时,将创建一个新对象(让我们将其称为isntance),该对象继承自
SomeFunc.prototype
引用的对象,该实例可通过
this
在该函数内部使用

现在,继承基本上归结为拥有一个原型(
SomeFunc.prototype
),它不是一个(几乎)空的对象(默认值),而是从另一个函数的原型继承的

示例:

function A(name) {
    // `this` refers to a new instance
    // lets set some properties:
    this.name = name;
}

// all "class" methods should be assigned to the prototype
A.prototype.getName = function() {
    return this.name;
};


// lets create a child "class"
function B(name, place) {
    // we have to call the parents constructor with the current instance
    // and the arguments we want to pass on.
    // this is like `super(name)` in Java or `A.__init__(self, name)` in Python 
    // (or `super(B, self).__init__(name)` in Python)
    A.call(this, name);
    this.place = place;
}

// here we connect A's prototype with B's prototype in a way that they
// stay independent 
inherits(B, A);

// B's "class" methods
B.prototype.getPlace = function() {
    return this.place;
};

// now we can do
var b = new B('SomeName', 'SomePlace');
alert(b.getName());
alert(b.getPlace());
这就是
inherits
的样子(此实现由Google闭包库使用):

您可以看到
Child.prototype
引用了
Tmp
的一个实例。但是
Tmp
的原型与
Parent
的原型相同。这意味着,
new Tmp()
返回的实例继承自
Parent
的原型,并且由于此实例是
Child
的原型,因此
Child
的所有实例也将继承自
Parent.prototype

使用ECMAScript 5,这可以简化为

Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
但本质上是一样的。它创建一个新对象,该对象继承自
Parent.prototype


进一步阅读:

function A(name) {
    // `this` refers to a new instance
    // lets set some properties:
    this.name = name;
}

// all "class" methods should be assigned to the prototype
A.prototype.getName = function() {
    return this.name;
};


// lets create a child "class"
function B(name, place) {
    // we have to call the parents constructor with the current instance
    // and the arguments we want to pass on.
    // this is like `super(name)` in Java or `A.__init__(self, name)` in Python 
    // (or `super(B, self).__init__(name)` in Python)
    A.call(this, name);
    this.place = place;
}

// here we connect A's prototype with B's prototype in a way that they
// stay independent 
inherits(B, A);

// B's "class" methods
B.prototype.getPlace = function() {
    return this.place;
};

// now we can do
var b = new B('SomeName', 'SomePlace');
alert(b.getName());
alert(b.getPlace());
这让我觉得我没有设置对象的原型,只是创建了一个名为prototype的属性,并将其设置为一个新的Sprite

对。此外,您根本没有使用函数的原型,因为您直接将每个函数分配给实例(
this.Draw=function()…
),而不是原型


在JavaScript中,有一种叫做构造函数的东西。使用
new
运算符调用的每个函数都是构造函数

以这种方式调用函数(
new SomeFunc()
)时,将创建一个新对象(让我们将其称为isntance),该对象继承自
SomeFunc.prototype
引用的对象,该实例可通过
this
在该函数内部使用

现在,继承基本上归结为拥有一个原型(
SomeFunc.prototype
),它不是一个(几乎)空的对象(默认值),而是从另一个函数的原型继承的

示例:

function A(name) {
    // `this` refers to a new instance
    // lets set some properties:
    this.name = name;
}

// all "class" methods should be assigned to the prototype
A.prototype.getName = function() {
    return this.name;
};


// lets create a child "class"
function B(name, place) {
    // we have to call the parents constructor with the current instance
    // and the arguments we want to pass on.
    // this is like `super(name)` in Java or `A.__init__(self, name)` in Python 
    // (or `super(B, self).__init__(name)` in Python)
    A.call(this, name);
    this.place = place;
}

// here we connect A's prototype with B's prototype in a way that they
// stay independent 
inherits(B, A);

// B's "class" methods
B.prototype.getPlace = function() {
    return this.place;
};

// now we can do
var b = new B('SomeName', 'SomePlace');
alert(b.getName());
alert(b.getPlace());
这就是
inherits
的样子(此实现由Google闭包库使用):

您可以看到
Child.prototype
引用了
Tmp
的一个实例。但是
Tmp
的原型与
Parent
的原型相同。这意味着,
new Tmp()
返回的实例继承自
Parent
的原型,并且由于此实例是
Child
的原型,因此
Child
的所有实例也将继承自
Parent.prototype

使用ECMAScript 5,这可以简化为

Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
但本质上是一样的。它创建一个新对象,该对象继承自
Parent.prototype


进一步阅读:

function A(name) {
    // `this` refers to a new instance
    // lets set some properties:
    this.name = name;
}

// all "class" methods should be assigned to the prototype
A.prototype.getName = function() {
    return this.name;
};


// lets create a child "class"
function B(name, place) {
    // we have to call the parents constructor with the current instance
    // and the arguments we want to pass on.
    // this is like `super(name)` in Java or `A.__init__(self, name)` in Python 
    // (or `super(B, self).__init__(name)` in Python)
    A.call(this, name);
    this.place = place;
}

// here we connect A's prototype with B's prototype in a way that they
// stay independent 
inherits(B, A);

// B's "class" methods
B.prototype.getPlace = function() {
    return this.place;
};

// now we can do
var b = new B('SomeName', 'SomePlace');
alert(b.getName());
alert(b.getPlace());