在Rajesh的答案的代碼是偉大的,但我想補充的關於JavaScript的封裝和「本」行爲的兩句話。
首先,在我的例子中,我不會使用'新',因爲它是邪惡的。 '新'迫使你在函數內部使用它,它的行爲並不真正透明。沒有它,你的代碼本身和範圍的使用將更容易理解。
大約有參照不同的變量的代碼內的兩個重要事實:
1)「這」指向內部使用它的對象。這可能導致混淆,因爲函數是第一類對象,但是子函數中的「this」指向父函數(Test())。不要在函數內使用'this'以避免混淆,請在對象內部使用它。
2)JavaScript具有函數範圍,這就是封裝如何工作的。內部函數可以看到外部對象中聲明的變量。在同名的內部函數中重新聲明變量會覆蓋外部變量,而不是創建具有更高優先級的內部副本。
如果你建立你的代碼,而無需使用「新」和「這個」裏面的功能,你的問題應該是不言自明:
function build_test_object() {
//This object will house all the functionality we will need to return
var that = {};
//This is a variable visible only while executing build_test_object
var prop1 = "one";
//display_prop1 is a function that is visible only inside build_test_object
//This way it will be protected after you use build_test_object: the function itself cannot be changed even if the object is altered
var display_prop1 = function() {
window.alert(prop1);
}
//And now we assign this function to the object we will return.
that.disp_prop = display_prop1;
//Returning the finished object
return that;
}
//Create the object
var test_object = build_test_object();
//Display the property
test_object.disp_prop();
注意如何PROP1和display_prop1不能從build_test_object碼外修改。即使替換test_object.disp_prop,它們也會保持不變(如果build_test_object中的其他方法使用prop1或display_prop1,則這很重要)。這就是所謂的閉包:build_test_object()已經解決了,而且它的函數範圍內沒有被暴露的東西是不可變的。
有關JavaScript中範圍,封閉和適當封裝方法的全面說明,我推薦Douglas Crockford編寫的「JavaScript:The Good Parts」,因爲這有點超出了本網站上某個問題的範圍。
Downvoter,如果我在答案中遺漏了某些東西,請隨時發表評論。會真的很感激它。 – Rajesh