我有這樣的功能:從對象範圍內的訪問對象的屬性JavaScript語法
function Entity(textureSrc)
{
var entity = {
texture: textureSrc,
position: { x: 0, y: 0 },
test: this.texture,
construct: function()
{
alert(this.test);
}
}
return entity;
}
然後這個測試代碼:
var testObject = Entity("Textures/AirTexture.png");
testObject.construct();
作爲一個測試,我想在創建entity
的新屬性時使用entity.texture
的值 - 我無法弄清楚這樣做的語法是什麼。
我已經試過:
test: this.texture
test: entity.texture
test: texture
但是,這些工作;它們全部導致undefined
。
另外 - 是否使用this
內的construct
方法正確訪問test
或應該以不同的方式完成?
好信息;那麼在我的例子中爲'test'賦值時,訪問'texture'的值時*解*是什麼? – Marty 2012-03-06 07:04:32
要麼使用與用於定義紋理(textureSrc)相同的來源,要麼定義測試一次* this *已被定義(例如,在this(this)中,this.test = this.texture)。 – Corbin 2012-03-06 07:06:59
啊,所以直到'this'被定義之前,沒有辦法引用屬性?有趣.. – Marty 2012-03-06 07:08:36