2012-05-16 42 views
0

我创建一个QScriptEngine并将QObject设置为具有一些信号/插槽的全局对象。然后我加载一些脚本文件并将其传递给引擎(使用评估)。该脚本创建一个对象并将全局对象的一些信号连接到它的函数。QtScript Javascript对象丢失属性

可悲的是,当它的函数从葛称为(它的设置过程中评估,我检查了)脚本对象的属性(this.password)被清零。

下面是脚本:

function Chanserv(password) { 
    this.password = password; 

// print("#### Constructor local: " + password + "/global: " + Bot.Password); 
} 

Chanserv.prototype.test = function() { 
// print("This is a test function/" + Bot.Password + "/" + this.password); 
} 

Chanserv.prototype.auth = function() { 
    print("#### entered auth function! " + this.password); 
// if (this.password && this.password.length > 0) { 
    if (Bot.Password && Bot.Password.length > 0) { 
     Bot.sendMessage("nickserv", "identify " + Bot.Password); 
//  print("Trying to authenticate with " + this.password); 
    } 
    else { 
     print("Bot.Password undefined."); 
//  print("this.password = " + this.password 
//    + ", this.password.length = " + (this.password.length > 0)); 
    } 
} 

var chanservObject = new Chanserv(Bot.Password); // this.password gets set 

chanservObject.test(); 
try { 
    Bot.joinedChannel.connect(chanservObject.auth); // this.password is empty when called... 
    Bot.joinedChannel.connect(chanservObject.test); 
// Bot.connected.connect(chanserv.auth); 
} 
catch (e) { 
    print(e); 
} 

任何想法,为什么这可能发生吗?

问候本

回答

0

JavaScript对象通过引用传递。在致电chanservObject.auth之前,您是否修改了Bot.Password

+0

Bot.Password之前脚本是负载和评估上的JavaScript侧设置。 代码看起来像'Bot.Password =“myPass”;'。 正如您在上一篇文章中看到的,即使函数被信号调用(用作回退),Bot.Password也是有效的。当函数被信号调用时,this.password是'undefined'。 – Ben

+0

@Ben是否有可能看到“Bot”代码? – 2012-05-16 22:47:45

+0

当然,http://github.com/xeviox-com/qirk,脚本是不是最新的,但僵尸代码本身应该是... – Ben