【发布时间】:2011-08-12 09:31:30
【问题描述】:
我的以下代码没有按预期运行:
var person = new Class({
initialize: function(name)
{
this.personName = name;
alert(this.personName) //WORKS :-)
this.testFunc(); //WORKS :-)
this.createShape(); //PAINTS SHAPE BUT CANNOT ACCESS 'personName'
},
testFunc() : function()
{
alert(this.personName);
},
createShape() : function()
{
this.personShape = paper.rect(40,40,40,40).attr({"fill":"blue"});
$(this.personShape.node).click(function()
{
alert(this.personName);
});
}
});
警报不适用于单击事件,我理解它,因为它无法访问对象变量“personName”。但是我想知道是否可以通过某种方式访问它?
是否有一个巧妙的 JavaScript 小技巧来实现这一点?
【问题讨论】:
标签: javascript jquery oop dom-events mootools