一年前,我创造了一些接近于此的东西。事实上,如果这是该领域的新事物,我已经通过搜索找到了这个线程。您可以在这里看到我创建的内容:https ://github.com/thiagomata/CanvasBox/blob/master/src/main/New.js
我的项目几乎是 100% OOP。所以,我用这个事实来集中我的解决方案。我使用名称“New”创建这个“类”,首先加载对象,然后再实例化对象。
这是某人使用它的示例:
var objSquare = New.Square(); // Square is loaded and after that instance is created
objSquare.x = objBox.width / 2;
objSquare.y = objBox.height / 2;
var objSomeExample = New.Stuff("some parameters can be sent too");
在这个版本中,我没有使用一些带有所有 js 文件位置的 json。正如您在此处看到的那样,映射是核心:
New.prototype.arrMap = {
CanvasBox: "" + window.MAIN_PATH + "CanvasBox",
CanvasBoxBehavior: "" + window.MAIN_PATH + "CanvasBoxBehavior",
CanvasBoxButton: "" + window.MAIN_PATH + "CanvasBoxButton",
// (...)
};
但是让这更自动化,使用 gulp 或 grunt 是我正在考虑做的事情,这并不难。
创建此解决方案是为了在项目中使用。因此,代码可能需要进行一些更改才能用于任何项目。但可能是一个开始。
希望这可以帮助。
正如我之前所说,这仍然是一个工作进展。但是我创建了一个更独立的模块,它使用 gulp 来保持更新。
所有的魔法都可以在这个链接中找到:
https ://github.com/thiagomata/CanvasBox/blob/master/src/coffee/main/Instance.coffee
https://github.com/thiagomata/CanvasBox/blob/master /src/node/scripts.js
https://github.com/thiagomata/CanvasBox/blob/master/gulpfile.js
Instance.coffee 的这一行应该有一个特殊的外观
###
# Create an instance of the object passing the argument
###
instaceObject = (->
ClassElement = (args) ->
window[args["0"]].apply this, args["1"]
->
ClassElement:: = (window[arguments["0"]])::
objElement = new ClassElement(arguments)
return objElement
)()
此行允许我在加载其文件后初始化某个对象的实例。在 create 方法中使用:
create:()->
@load()
return instaceObject(@packageName, arguments)