通过另一个JavaScript文件添加到函数

通过另一个JavaScript文件添加到函数

本文介绍了通过另一个JavaScript文件添加到函数中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我想知道如何添加到我的函数draw();我的画布引擎中使用绘制来更新其中的所有内容。我想要做的是创建一个独立的引擎,可以说不做编辑,而是更新完全与它相关的新事物。例如 -

Hey I was wondering how I could add on to my function draw(); draw is used in my canvas engine to update everything within it. What I want to do is create a standalone engine that could say be left un-edited and yet update completely new things just linked to it. For example-

function draw(){
    gameloop();
    cameraWrapper();
    context2D.clearRect(0,0,canvas.width, canvas.height);
}

现在说我创建一个新应用程序并使用此引擎..我想能够创建一个独立的文件链接到引擎说一个球员对象。

Now say I create a new app and use this engine.. I want to be able to just create a standalone file linked to the engine say a player object.

player = new object();
function playerupdate(){
     stuff;
     stuff;
}

现在我怎么说可以将playerupdate()函数添加到engine.js中draw()函数而不编辑engine.js文件?这会像原型吗?如果是这样,即使它不是和示例将不胜感激!

如果您有任何问题,请提问,

提前致谢!

Now how could I say add the playerupdate() function into the engine.js's draw() function without editing the engine.js file? would this be like a prototype? if so and even if its not and example would be greatly appreciated!

If you have any questions please ask,

thanks in advance!

推荐答案

I认为继承有点太复杂了......你可以通过钩子来实现所有你想要的。

I think inheritance is a bit too complex for this... you can achieve all of what you want with just hooks.

试试这样:

var postDrawHooks = [];
var draw = function(){
    // do stuff
    postDrawHooks.forEach(function(hook){hook()});
}

var playerUpdate = function(){...};
postDrawHooks.push(playerUpdate);

这篇关于通过另一个JavaScript文件添加到函数中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 05:45