I am using the Canvas 2d context to write text to the screen..
To accomplish this I have it run through an array of text objects that I created, right now I have the text objects with 3 properties:
text.text
text.x
text.y
text.text holds the string to write, text.x holds the value for the x position, and text.y holds the value for the y position
Is there anyway I could skip the text.text property?
so for example, right now it looks something like this:
var textStrings = [];
textStrings[0] = {};
textStrings[0].text = "hello";
textStrings[0].x = 0;
textStrings[0].y = 10;
textStrings[1] = {};
textStrings[1].text = "world";
textStrings[1].x = 10;
textStrings[1].y = 10;
But is there any way that I could do something like this instead:
textStrings = [];
textStrings[0] = {};
textStrings[0] = "hello";
textStrings[0].x = "0";
textStrings[0].y = 10;
textStrings[1] = {};
textStrings[1] = "world";
textStrings[1].x = 10;
textStrings[1].y = 10;
basically a default property of an object or something…
right now as soon as I do something like
textStrings[0] = "hello";
it changes textStrings to a string instead of an object, and then I can no longer add properties to it, since its a primitive data type.
Thanks
You can use String objects instead of primitive values:
You might also use special objects. The
toStringmethod is automatically used when the object is to be converted to a string:I guess you would have such a constructor function anyway, to simplify the syntax for the
textStringsarray.