EaselJS中的动态阶段名称 [英] Dynamic Stage name in EaselJS
问题描述
我目前正在设计一个网站,旨在同时显示多个画布,而不知道哪些画布将提前显示(PHP脚本创建列表)。
I'm currently working on a site designed to display multiple canvases at the same time, without knowing which canvases it's going to display in advance (a PHP script creates the list).
这个想法是有多个脚本文件(每个画布一个)被(通常)一个单独的处理程序读取,这将填充各个阶段。
The idea is to have multiple script files (one per canvas) be read by (typically) a single handler which will populate the individual stages.
stage = new createjs.Stage(context);
我被困在这里。当我创建一个给定的阶段,我知道画布的名称,我想附加到它(例如,stageA),它在JavaScript中的变量命名上下文中可用。在这种情况下,我需要创建一个名为stageA的easeljs阶段;问题是我不能预先知道我不需要stageB而不是stageA所以创建阶段的名称需要是动态的(等于上下文)。我试着沿着
I'm stuck here. At the time I create a given stage, I know the name of the canvas I want to attach it to (for instance, stageA) and it's available in JavaScript in the variable named context. In this situation, I need to create an easeljs stage named stageA; the problem is I can't know in advance I won't need stageB instead of stageA so the created stage's name needs to be dynamic (equal to context). I tried something along the lines of
eval(context) = new createjs.Stage(context);
但不能正常工作。
(我还需要能够在这个动态命名的阶段上操作,即能够执行
(I also need to be able to operate on this dynamically named stage, ie be able to do
stageA.update();
例如,不知道stageA被称为stageA,而只是基于上下文。)
for instance, without knowing stageA is called stageA but just based on context.)
任何建议?
推荐答案
可以使用括号符号作为全局变量, code> windows [stageA] 或者如果你知道你在全局范围 this [stageA]
You can use bracket notation for global variables with either windows["stageA"]
or if you know you are in global scope this["stageA"]
因此,如果你在全局范围内有一个上下文,并且你有一个包含其变量名的字符串,那么你可以通过括号符号访问它。
So if you have a context in global scope and you have a string with its variable name then you can access it via bracket notation like so
var stageA = // context of canvas
var stageName = "stageA"
var theStage = window[stageName];
现在 theStage
参考 stageA
。
theStage.clearRect(0,0,100,100);
// same as
stageA.clearRect(0,0,100,100);
所有对象属性都可以通过点符号 $ c> window.alert 或 braket符号即 window [alert]
。
All object attributes can be accessed via either dot notation ie window.alert
or braket notation ie window["alert"]
. As braket notation takes a string you can use a variable in its place.
另一个例子
var myObj = {
attributeA : 10,
}
console.log(myObj.attributeA); // output 10;
console.log(myObj["attributeA"]); // output 10;
var attributeName = "attributeA";
console.log(myObj[attributeName]); // output 10;
全局范围称为 window
人们放下窗口并直接引用全局范围。 EG window.alert
与 alert
或相同window.requestAnimationFrame
与 requestAnimationFrame相同
The global scope is called window
but most people drop the window and directly reference the global scope. EG window.alert
is the same as alert
or window.requestAnimationFrame
is the same as requestAnimationFrame
全局作用域也称为 code>,但是你需要知道你在全局范围。如果您有信心,则
此[alert]
或窗口[alert]
或 alert
或 window.alert
或 this.alert
都在全局范围内。
The global scope is also called this
but you will need to know you are in global scope. If you are confident then this["alert"]
or window["alert"]
or alert
or window.alert
or this.alert
are all the same reference if you are in global scope.
这篇关于EaselJS中的动态阶段名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!