简体   繁体   English

Javascript - 原型函数数组

[英]Javascript - Array of prototype functions

I'm a javascript newbie so I'm writing ugly code so far sometimes due to my lack of experience and how different it is to the languages I'm used to, so the code I'll post below works, but I'm wondering if I'm doing it the right way or perhaps it works but it's a horrible practice or there is a better way. 我是一个javascript新手,所以到目前为止我写的丑陋代码有时候由于我缺乏经验以及它与我习惯的语言有多么不同,所以下面我要发布的代码有效,但我是想知道我是以正确的方式做到这一点,还是可行,但这是一种可怕的做法,或者有一种更好的方法。

Basically, I have a little dude that moves within a grid, he receives from the server an action, he can move in 8 directions (int): 0:up, 1: up-right, 2: right... 7: up-left. 基本上,我有一个在网格内移动的小家伙,他从服务器接收一个动作,他可以​​在8个方向移动(int):0:向上,1:向上 - 向右,2:向右...... 7:向上-剩下。

the server will send him this 0 <= action <= 7 value, and he has to take the correct action... now, instead of using a switch-case structure. 服务器将向他发送此0 <= action <= 7值,并且他必须采取正确的操作...现在,而不是使用switch-case结构。 I created a function goUp(), goLeft(), etc, and loaded them in an array, so I have a method like this: 我创建了一个函数goUp(),goLeft()等,并将它们加载到一个数组中,所以我有一个像这样的方法:

var getActionFunction = actions[action];
actionFunction();

However, what to set all this up is this: 但是,如何设置这一切是这样的:

1) create a constructor function: 1)创建构造函数:

function LittleDude(container) {
    this.element = container; //I will move a div around, i just save it in field here.
}

LittleDude.prototype.goUp() {
    //do go up
    this.element.animate(etc...);
}

LittleDude.prototype.actions = [LittleDude.prototype.goUp, LittleDude.prototype.goUpLeft, ...];
//In this array I can't use "this.goUp", because this points to the window object, as expected

LittleDude.prototype.doAction = function(action) {
    var actionFunction = this.actions[action];
    actionFunction(); //LOOK AT THIS LINE
}

Now if you pay attention, the last line won't work.. because: when i use the index to access the array, it returns a LittleDude.prototype.goUp for instance... so the "this" keyword is undefined.. 现在,如果你注意,最后一行将无法工作..因为:当我使用索引访问数组时,它返回一个LittleDude.prototype.goUp例如...所以“this”关键字是未定义的..

goUp has a statement "this.element"... but "this" is not defined, so I have to write it like this: goUp有一个语句“this.element”...但是“this”没有定义,所以我必须这样写:

actionFunction.call(this);

so my doAction will look like this: 所以我的doAction将如下所示:

LittleDude.prototype.doAction = function(action) {
    var actionFunction = this.actions[action];
    actionFunction.call(this); //NOW IT WORKS
}

I need to know if this is hackish or if I'm violating some sort of "DO NOT DO THIS" rule. 我需要知道这是否是hackish,或者我是否违反了某种“不要做这个”的规则。 or perhaps it can be written in a better way. 或者也许它可以用更好的方式编写。 Since it seems to me kind of weird to add it to the prototype but then treating it like a function that stands on its own. 因为在我看来有点奇怪的是将它添加到原型中然后将其视为一个独立的功能。

What you are trying to do is one of the possible ways, but it is possible to make it more simple. 你想要做的是一种可能的方法,但它可以使它更简单。 Since object property names are not necessary strings, you can use action index directly on prototype. 由于对象属性名称不是必需的字符串,因此可以直接在原型上使用动作索引。 You even don't need doAction function. 你甚至不需要doAction功能。

LittleDude = function LittleDude(container) {
  this.container = container;
}

LittleDude.prototype[0] = LittleDude.prototype.goUp = function goUp() {
  console.log('goUp', this.container);
}

LittleDude.prototype[1] = LittleDude.prototype.goUpRight = function goUpRight() {
  console.log('goUpRight', this.container);
}

var littleDude = new LittleDude(123),
    action = 1;
littleDude[action](); // --> goUpRight 123
littleDude.goUp(); // --> goUp 123
 actionFunction.call(this); //NOW IT WORKS 

I need to know if this is hackish or if I'm violating some sort of "DO NOT DO THIS" rule. 我需要知道这是否是hackish,或者我是否违反了某种“不要做这个”的规则。 or perhaps it can be written in a better way. 或者也许它可以用更好的方式编写。

No, using .call() is perfectly fine for binding the this keyword - that's what it's made for. 不,使用.call()完全可以绑定this关键字 - 这就是它的用途。

Since it seems to me kind of weird to add it to the prototype but then treating it like a function that stands on its own. 因为在我看来有点奇怪的是将它添加到原型中然后将其视为一个独立的功能。

You don't have to define them on the prototype if you don't use them directly :-) Yet, if you do you might not store the functions themselves in the array, but the method names and then call them with bracket notation : 如果你不直接使用它们,你不必在原型上定义它们:-)但是,如果你这样做,你可能不会将函数本身存储在数组中,但方法名称然后用括号表示法调用它们:

// or make that a local variable somewhere?
LittleDude.prototype.actions = ["goUp", "goUpLeft", …];

LittleDude.prototype.doAction = function(action) {
    var methodName = this.actions[action];
    this[methodName](); // calls the function in expected context as well
}

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

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