English 中文(简体)
Javascript Prototype Problems
原标题:Javascript Prototype Problems

我的情况是,我正在利用Box2DWeb开展一场Canvas游戏,因此,我有一个名为Sprite>/em>的班子,为Canvas树立了形象,我现在想创建Physics Sp<>。 页: 1

class:

Sprite = function (position, dimension, image) {

  this.Position = position;
  this.Dimension = dimension;
  if (image)
    this.Image = image;
  ...

  this.Draw = function (g) {
    ...
  }
  ...
};

而我是在物理学家(物理学家为......js)中树立的原型:

PhysicsSprite = function(position, dimension, world) {

    this.prototype = new Sprite(position, dimension, undefined);
    //alert(this.prototype.Position.X)

    var body = CreateBody();
    this.GetBody = function () { return body; }

    this.Draw = function (g) {
        ...  
    }

    function CreateBody () {
          ...
    }
};

Note the alert (this.prototype.Position.X), which does press me of the status, but if I change the Code to this.Position. X,我有了一个错误,同样,如果我有Physics Sprite>,我必须明确标明原型。

由此,我想,我有的放矢地树立了标本,只是创造了一个叫做prototype<>/code>的财产,并且将这种财产设定为一个新的Sprite

If someone could help me, and explain what I am doing wrong, that would be much appreciated. I am dyslexic, so I always misspell variables, which is frustrating, so that was the first thing I looked for, but everything looks fine to me.

最佳回答

这使我想,我 t了目标原型,仅仅创造了一个称为原型的财产,并且为新的标准创造了条件。

这一权利。 此外,你根本不使用功能原型,因为你将每个功能直接分配给具体情况(<代码>)。 提取=功能()而不是原型。


在 Java文中,你有点叫-buildingorFunction。 “new操作者”所要求的每一功能都是建筑功能。

当一项功能被称作“new SomeFunc()”时,就产生了一个新的物体(其名称为isntance),该物体继承了所指的物体。 一些Func.prototype ,这一实例可在该职能范围内通过this查阅。

现在,遗产基本上是为了拥有一个原型(SomeFunc.prototype),而不是一个(直截了当的)空物体(违约),而是继承另一个功能原型。

Example:

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。 谷歌关闭图书馆也采用这种方式:

function inherits(Child, Parent) {
    var Tmp = function() {};
    Tmp.prototype = Parent.prototype;
    Child.prototype = new Tmp();
    Child.prototype.constructor = Child;
}

参看<代码>Child.prototype。 Tmp。 But Tmp 原型与<代码>相同。 父母原型。 这意味着,通过<代码>new Tmp(>继承Parent原型而退还的,由于这一实例是<代码>Child的原型,所有<代码>Child的事例也将从Parent.prototype继承。

有了ECMA第5版,这可以简化。

Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;

但它基本上相同。 它产生一个新的物体,继承<代码>Parent.prototype。


www.un.org/spanish/ecosoc 进一步读:

问题回答

Change

this.pro页: 1type = new Sprite(position, dimension, undefined);

页: 1

Sprite.apply(this, position, dimension, undefined);

You are right, before the change, you are assigning an instance of object 页: 1 the "pro页: 1type" property.

The pro页: 1type is a property of the construc页: 1r function, not an object instance - i.e. you can access pro页: 1type by PhysicsSprite.pro页: 1type, but not this.pro页: 1type or x.pro页: 1type (where x = new PhysicsSprite()).

Java贝是一种与 Java截然不同的鸟类,尽管它们相互之间经常发生错觉,因为两者都分享{和}。

典型的 Java本类:

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) {
        // ...
    }
};

Java的继承非常令人望而却。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.