2012-07-04 71 views
0

所以我想在我的構造函數中有一個方法。我打電話給我的構造類似這樣的如何在Java腳本構造函數中定義Java腳本方法

new EnhancedTooltip($("taskPreview1")) 

,並把它定義爲這樣

///<var> Represents the controls</var> 
var EnhancedTooltip = function (toolTipObject) { 

    ///<field name="alignment" type="Number">.</field> 
    this.alignment = 2; 
    ///<field name="associatedControl" type="Number">.</field> 
    this.associatedControl = toolTipObject; 
    ///<field name="caption" type="Number">.</field> 
    this.caption = "Let's see how this works"; 
    ///<field name="description" type="Number">.</field> 
    this.description; 
    ///<field name="enableEffects" type="Number">.</field> 
    this.enableEffects = false; 
    ///<field name="fadeAnimationSpeed" type="Number">.</field> 
    this.fadeAnimationSpeed = 500; 
    ///<field name="image" type="Number">.</field> 
    this.image; 
    ///<field name="minimumHeight" type="Number">.</field> 
    this.minimumHeight = 100; 
    ///<field name="minimumWidth" type="Number">.</field> 
    this.minimumWidth = 200; 
    ///<field name="objectName" type="String">.</field> 
    this.objectName = $("taskToolTip"); 
    ///<field name="padding" type="Number">.<field> 
    this.padding = 5; 
    ///<field name="tailAngle" type="Number">.</field> 
    this.tailAngle; 
    ///<field name="tailDiamensions" type="Object">.</field> 
    this.tailDimensions = new Size(15, 0); 

    EnhancedTooltip.init(); 

    this.init = new function() { 
     ///<summary>Initializes the class EnhancedTooltip.<summary> 

     console.log(EnhancedToolTip.objectName); 
    }; 

    }; 

,並獲得該EnhancedTooltip有沒有方法初始化錯誤。那麼我應該如何定義init。

另外,如果我想從init調用其他函數,我會怎麼做。例如

this.addToolTipElements = function() { 

    $("#workspaceContainer").append("<div id = taskToolTip class = taskToolTip</div>"); 

    this.objectName.append("<canvas id = toolTipCanvas class = toolTipCanvas </canvas>"); 


    this.objectName.append("<div id = toolTipCaption class = toolTipCaption>" + this.caption + "</div>"); 

}; 

回答

1

這是因爲「EnhancedTooltip」是您的構造函數,並且它沒有「init」屬性。然而,正在建設中的對象(this)作用:

this.init(); 

,並在函數內部:

console.log(this.objectName); 

而且,它不是new function,它只是function

this.init = function() { 
    console.log(this.objectName); 
}; 

編輯啊,正如Sirko指出的那樣,在之後調用函數定義它。

+0

現在,如果我想從調用函數初始化如何將我做 –

+0

當調用「初始化」是這樣的:'this.init();',或從外面:'myTooltip。 init();',然後'this' * inside *函數將引用您的對象實例。因此,你總是可以通過'this' - 'this.someMethod();'訪問你在構造函數或對象原型中設置的方法。 – Pointy

0

由於您正在使用函數表達式,因此您不能依賴此處的變量提升。

只要定義方法,調用它像這樣前:

// rest of the code  

///<field name="tailDiamensions" type="Object">.</field> 
this.tailDimensions = new Size(15, 0); 

this.init = new function() { 
    ///<summary>Initializes the class EnhancedTooltip.<summary> 

    console.log(this.objectName); 
}; 

this.init(); 

在一個旁註,你應該只使用this在構造函數中引用你的對象。

0

首先你打電話init()方法之前你聲明它。

而第二件事是 - 嘗試使用更好的風格來聲明Clases。

例如,我更喜歡this

0

您正在調用init錯誤。您在this範圍內定義了它,而不是原型,因此EnhancedToolTip在創建之前沒有此功能。試試這個:

var EnhancedTooltip = function (toolTipObject) { 
    [..] 

    this.init = new function() { 
    ///<summary>Initializes the class EnhancedTooltip.<summary> 
    console.log(this); 
    }; 

    this.init(); 
};