2013-05-16 95 views
1

使用變量從一個文件,所以,我建立一個相當複雜的插件,跑進一個障礙。 (這是我如何學習的,所以斷枝都不錯......)我的代碼看起來類似於以下內容:的javascript:在其他

文件1

if(!window.TextEdit){ 
    var TextEdit = {"version": "1.0"}; 
} 

TextEdit.edit = function(context, options) { 
    var self = this; 
    self.context = context; 

    self.buttonDef = { 
     bold: { 
      class: 'bold', 
      command: 'bold', 
      icon: 'bold', 
      type: 'checkbox', 
      label: '' 
     }, 
     italic: { 
      class: 'italic', 
      command: 'italic', 
      icon: 'italic', 
      type: 'checkbox', 
      label: '' 
     }, 
     underline: { 
      class: 'underline', 
      command: 'underline', 
      icon: 'underline', 
      type: 'checkbox', 
      label: '' 
     } 
    } 

    self.init(); 
} 

文獻2

if(!window.TextEdit.imageload){ 
    TextEdit.imageload = {"version": "1.0"}; 
} 

TextEdit.imageload = function() { 
    var self = this; 
    self.editor = TextEdit; 

    self.init(); 
} 

TextEdit.imageload.prototype = { 
    init: function() { 
     var self = this; 

     console.log(self.buttonDef); 

     $('.tdt-btn-addimage').click(function() { 



     }); 


    }, 
    create: function() { 

    }, 
    destroy: function() { 

    } 
} 

new TextEdit.imageload(); 

因此,使用Document 2我想訪問文檔1中的變量self.buttonDef。我可以很好地訪問Document 1中的函數,但不能訪問變量。

我所尋找的是如何讓buttonDefTextEdit的屬性。

回答

1

您可以使用buttonDef下面的代碼文本編輯的屬性。

TextEdit.buttonDef = { 
     bold: { 
      class: 'bold', 
      command: 'bold', 
      icon: 'bold', 
      type: 'checkbox', 
      label: '' 
     }, 
     italic: { 
      class: 'italic', 
      command: 'italic', 
      icon: 'italic', 
      type: 'checkbox', 
      label: '' 
     }, 
    underline: { 
     class: 'underline', 
     command: 'underline', 
     icon: 'underline', 
     type: 'checkbox', 
     label: '' 
    } 
}; 
+0

謝謝:)它的工作 – watzon

-1

我認爲問題在於self位於函數edit的內部,因此無法在第二個文檔的範圍內進行訪問。考慮讓您的edit函數返回它創建的對象self,將其作爲變量存儲,然後調用var.buttonDef

+0

我想返回自我並訪問它,但我沒有得到任何結果。你能舉個例子嗎? – watzon