简体   繁体   English

类仿真中的JS和方法

[英]JS and methods in class emulation

I am new to this so I'll try to be as detailed as possible. 我对此并不陌生,所以我将尽可能详细。

I am creating a JS class using functions. 我正在使用函数创建一个JS类。

function Bandstar(p, id)
{
  var numberOfSides, Xcenter, Ycenter;
  var canvas;
  var circlesInt;
  var linesInt;
  var lines;
  var linesInt2;
  var linesMidToEdge; 
.....

When it comes to methods, I just declare them like this: 对于方法,我只是这样声明它们:

this.IAset = function(a) 
{
   for (var i =0; i<= this.numberOfSides-1;i++)
   {
      if (parseInt(a[i]) == a[i])
         this.circles[i].value = a[i];
      else
         return false;
    }

    this.IArepaint();
    return true
}

and that's pretty much it. 就是这样。

The problem now is that, when I am creating a specific method: 现在的问题是,当我创建特定方法时:

this.moveFunc = function(e)
{
   var p = e.target;
   this.IAmove(p);
};

with the method IAmove being declared as follows: 使用方法IAmove声明如下:

this.IAmove = function(p)
{
   var m = (p.oTop - this.Ycenter ) / (p.oLeft - this.Xcenter);
   var linea = m * ( p.left - p.oLeft) + p.oTop;
   var ejeX = p.left;
   ...
} 

The compiler just keeps throwing this error: 编译器只是不断抛出此错误:

Uncaught TypeError: Object function (e)   {     var p = e.target;     this.IAmove(p);   } has no method 'IAmove'

So, the thing is that I am declaring moveFunc as a function but then when I try to get to use this property, it doesn't actually use the instance of the class above it, ( Bandstar ), but of itself (and obviously moveFunc doesn't have a method called IAmove , the class on which it is being created is)... 所以,事情是我将moveFunc声明为一个函数,但是当我尝试使用this属性时,它实际上并没有使用其Bandstar类的实例( Bandstar ),而是使用了其自身(显然是moveFunc没有称为IAmove的方法,正在创建该方法的类是)...

I thought this was just like it worked, but I must not be getting the concept of heritage and class morphology in JS right. 我以为这就像工作一样,但是我一定不能正确理解JS中的继承和类形态的概念。

How can I access a method in a class from another method inside that same class? 如何从同一个类中的另一个方法访问一个类中的方法? If I just write IAmove(p) it will just say the method IAmove() is undefined (because it isn't, it's part of the Bandstar namespace. 如果我只写IAmove(p)它只会说IAmove()方法是未定义的(因为它不是,它是Bandstar名称空间的一部分)。

I know it must be something stupid that I'm not seeing. 我知道这一定是我没有看到的愚蠢的事情。 Any suggestions? 有什么建议么?

The context inside your function may change based on your call. 函数内部的上下文可能会根据您的调用而改变。 So this could not be the original object. 因此, this可能不是原始对象。

You should add a private var like: 您应该添加一个私有变量,例如:

 function Bandstar(p, id)
{
  var numberOfSides, Xcenter, Ycenter;
  var canvas;
  var that = this; // add this line

and use it when you try to call function (or get properties) of original object 并在尝试调用原始对象的函数(或获取属性)时使用它

this.moveFunc = function(e)
  {
    var p = e.target;
    that.IAmove(p); // change this line
  };

for be sure to point to original object. 因为一定要指向原始对象。

EDIT: According with comments you can read more infos in Stuart's answer 编辑:根据评论,您可以在斯图尔特的答案中阅读更多信息

Assuming all of those methods are defined in the constructor the syntax is correct . 假设所有这些方法都在构造函数中定义, 则语法正确 The following fiddle illustrates it working, the code is based on a simplified version of your code... 以下小提琴说明了它的工作原理 ,该代码基于您的代码的简化版本...

function Test() {
    this.move = function(p) {
        document.write("Moved to: " + p);
    };
    this.another = function(e) {
        this.move(e.target);
    };
}

var test = new Test();
test.another({target: "The moon"});

The cause of the issues is likely to be one of two three things: 这些问题的原因很可能是 两种 三种情况之一:

  1. How the method moveFunc is called 如何调用moveFunc方法
  2. When the method moveFunc is called 当调用moveFunc方法时
  3. How the method IAmove is called (it may not be the moveFunc function that is causing that error). IAmove方法的调用方式(可能不是导致该错误的moveFunc函数)。

The this keyword will reference the scope of the function which is typically the owner of the method... this关键字将引用函数的范围,该函数通常是方法的所有者。

var bandstar = new Bandstar(p, id);
bandstar.moveFunc(e); // this should be bandstar

The only reasons that might not be the case is if you are explicitly binding the moveFunc function to another object or a more common situation is that the function is being called without being attached to the owner... 可能不是这种情况的唯一原因是,如果您将moveFunc函数显式绑定到另一个对象,或者更常见的情况是调用该函数而不将其附加到所有者...

var bandstar = new Bandstar(p, id);
var moveFunc = bandstar.moveFunc(e);
moveFunc(e); // this will now be window

The this keyword will default to window if the owner is detached from it. 如果所有者与之分离,则this关键字将默认为window You can bind the function using. 您可以使用绑定功能。

var bandstar = new Bandstar(p, id);
var moveFunc = bandstar.moveFunc(e);
moveFunc.bind(bandstar);
moveFunc(e); // this will now be bandstar again

In the second issue both methods must be defined in the constructor before the moveFunc function can be called 在第二个问题中,必须先在构造函数中定义两个方法,然后才能调用moveFunc函数

this.moveFunc = function(e) { ... }
this.moveFunc(e); // Will throw an error that IAmove does not exist in the object
this.IAmove = function(p) { ... }

You can log the this object using console.log(this); 您可以使用console.log(this);记录this对象console.log(this); to find out what it is. 找出它是什么。

It looks as though from your error that you are trying to call IAmove on the moveFunc function. 从错误看来,您正在尝试在moveFunc函数上调用IAmove For this error to be caused you would need to have called moveFunc with itself as the owner or the bound object... Which is not a particularly likely. 要引起此错误,您需要以自身作为所有者或绑定对象的方式调用moveFunc ...不太可能。

I have a hunch that the error may not actually be related to the this.IAmove(p); 我有一种预感,该错误实际上可能与this.IAmove(p); call in the moveFunc as assumed. 按假设在moveFunc调用。 Post the stack trace and we can use that to find where the error occurred. 发布堆栈跟踪 ,我们可以使用它来查找发生错误的位置。

You probably wants to add these methods to your Bandstar prototype . 您可能希望将这些方法添加到Bandstar原型中

function Bandstar(p, id)
{
   // ...
}

Bandstar.prototype.IAset = function(a) 
{
   // ...
}

Bandstar.prototype.moveFunc = function(e)
{
   var p = e.target;
   this.IAmove(p);
};

This way your methods keep the Bandstar context, and this will remain Bandstar instance reference. 这样,你的方法保持Bandstar上下文, this将保持Bandstar实例引用。

Have a look at Object.prototype reference 看看Object.prototype参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM