Javascript 对象的默认值

Javascript 对象的默认值,javascript,Javascript,我正在使用画布2d上下文将文本写入屏幕 为了实现这一点,我让它运行我创建的文本对象数组,现在我有了具有3个属性的文本对象: text.text text.x text.y text.text保存要写入的字符串,text.x保存x位置的值,text.y保存y位置的值 我是否可以跳过text.text属性 例如,现在它看起来像这样: var textStrings = []; textStrings[0] = {}; textStrings[0].text = "hello"; textStri

我正在使用画布2d上下文将文本写入屏幕

为了实现这一点,我让它运行我创建的文本对象数组,现在我有了具有3个属性的文本对象:

text.text
text.x
text.y
text.text保存要写入的字符串,text.x保存x位置的值,text.y保存y位置的值

我是否可以跳过text.text属性

例如,现在它看起来像这样:

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;
但有没有办法让我做这样的事:

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;
基本上是一个对象或某物的默认属性

现在只要我做了类似的事情

textStrings[0] = "hello";
它将textStrings更改为字符串而不是对象,然后我无法再向其添加属性,因为它是一种基本数据类型


谢谢

如果保证您的文本字符串是唯一的,您可以将其用于索引:

var textStrings = {};
textStrings["world"].x = 10;
textStrings["world"].y = 10;
然后,您可以使用以下代码段获得“文本”字符串的列表(以便可以通过对象进行索引):

textkeys : function  (textStrings) {
   var accumulator = [];
   for (var propertyName in o) {
      arr.push(propertyName);
   }
   return accumulator;
}

中,如果保证您的文本字符串是唯一的,您可以使用它们编制索引:

var textStrings = {};
textStrings["world"].x = 10;
textStrings["world"].y = 10;
然后,您可以使用以下代码段获得“文本”字符串的列表(以便可以通过对象进行索引):

textkeys : function  (textStrings) {
   var accumulator = [];
   for (var propertyName in o) {
      arr.push(propertyName);
   }
   return accumulator;
}

中,可以使用字符串对象而不是基本值:

var a = new String("hello");
a.x = "0";
a.y = 10;
var b = new String("world");
b.x = "10";
b.y = 10;

var textStrings = [a, b];
您也可以使用特殊对象。将对象转换为字符串时,将自动使用
toString
方法:

function Text(t, x, y) {
    this.text = t; this.x = x; this.y = y;
}
Text.prototype.toString = function() { return this.text; }
alert(new Text("hello", 0, 0)); // implicit string conversion
console.log(""+new Text("world", 10, 10)); // explicit string conversion

我想您无论如何都会有这样一个构造函数,以简化
textStrings
数组的语法。

您可以使用字符串对象而不是原始值:

var a = new String("hello");
a.x = "0";
a.y = 10;
var b = new String("world");
b.x = "10";
b.y = 10;

var textStrings = [a, b];
您也可以使用特殊对象。将对象转换为字符串时,将自动使用
toString
方法:

function Text(t, x, y) {
    this.text = t; this.x = x; this.y = y;
}
Text.prototype.toString = function() { return this.text; }
alert(new Text("hello", 0, 0)); // implicit string conversion
console.log(""+new Text("world", 10, 10)); // explicit string conversion

我想您无论如何都会有这样一个构造函数,以简化
textStrings
数组的语法。

类似于@Bergi的思想,但使用函数方法创建对象:

var textCreator = function (text, x, y) {
    var that = {};

    that.text = text;
    that.x = x;
    that.y = y;

    that.toString = function () {
        return text;
    }

    return that;
};

textStrings = [];
textStrings[0] = textCreator("hello", 0 ,10);

textStrings[1] = textCreator("world", 10, 10);
为了完整性:您可以扩展
字符串
原型,但这是不可取的,实际上与Bergi提供的一个解决方案相同

String.prototype.x = null;
String.prototype.y = null;

textStrings = [];
//textStrings[0] = "hello";
textStrings[0] = new String("hello");
textStrings[0].x = "0";
textStrings[0].y = 10;

//textStrings[1] = "world";
textStrings[1] = new String("world");
textStrings[1].x = 10;
textStrings[1].y = 10;

与@Bergi的思想类似,但使用函数方法创建对象:

var textCreator = function (text, x, y) {
    var that = {};

    that.text = text;
    that.x = x;
    that.y = y;

    that.toString = function () {
        return text;
    }

    return that;
};

textStrings = [];
textStrings[0] = textCreator("hello", 0 ,10);

textStrings[1] = textCreator("world", 10, 10);
为了完整性:您可以扩展
字符串
原型,但这是不可取的,实际上与Bergi提供的一个解决方案相同

String.prototype.x = null;
String.prototype.y = null;

textStrings = [];
//textStrings[0] = "hello";
textStrings[0] = new String("hello");
textStrings[0].x = "0";
textStrings[0].y = 10;

//textStrings[1] = "world";
textStrings[1] = new String("world");
textStrings[1].x = 10;
textStrings[1].y = 10;

你知道你可以使用
textStrings[0]={text:'foo',x:42,y:42}。甚至
var textStrings=[{text:'foo',x:42,y:42},{text:'foo2',x:42,y:42}]textStrings[0]={text:'foo',x:42,y:42}。甚至
var textStrings=[{text:'foo',x:42,y:42},{text:'foo2',x:42,y:42}]字符串原型方法将不起作用。[数组中]的值仍然是基本值,每次访问属性时都会转换为不同的字符串对象。和
“”.x
始终打印
null
。字符串原型方法将不起作用。[数组中]的值仍然是基本值,每次访问属性时都会转换为不同的字符串对象。和
”.x
始终打印
null