2012-05-24 27 views
0

有兩件事情我沒有在這個場景理解太清楚如何調用一個函數在Page.ClientScript正確

首先,有一個javascript是這種格式

function Allocate() 
{ 
    this.Name = $("allocateName"); 
    this.AddAllocation = $("addAllocation"); 
    this.setCustom= $("setCustom"); 
} 
... some other initializations here 
Allocate.prototype.EnableAllocations = function() { 

this.enableAllAlloactions(); 
this.setCustomAllocations(); 
} 

這是隻是一個例子,這是什麼類的Javascript類?這是在一個名爲Allocate.js的文件中。

我的問題有以下幾點:

如果我打電話給在Page.ClientScript.RegisterClientScriptBlock(...);我會怎麼做,該EnableAllocations?

回答

0

你應該實例的分配對象,並調用其EnableAllocations功能。

var alloc = new Allocate(); 
    alloc.EnableAllocations; 

所以,你可以創建THA一串代碼,並使用這樣的傳遞:

Page.ClientScript.RegisterClientScriptBlock("key", 
    "var alloc = new Allocate();alloc.EnableAllocations;"); 
  • 注意:您可以使用this.GetType()全名的關鍵。你不應該爲不同的腳本使用相同的密鑰。

此方法已。使用ClientScriptManager.RegisterClientScriptBlock方法代替(它取決於您使用的框架的版本)。

最後,我不知道你使用的庫,但$()來自一些庫。因此,有對庫的依賴性,還取決於allocateNameaddAllocation等..不管他們。

相關問題