我試圖改變某種方法來將方法調用到命名空間中。javascript命名空間調用其他函數方法
- 調用父方法(我不認爲它有可能)
- 創建和調用繼承功能
- 內的另一個方法調用(主要是jQuery的onReady事件功能)(this.MyFunction()不工作)
我分裂文件中的每個命名空間(希望繼續保持這種方式)
我嘗試How to call function A from function B within the same namespace?但我沒有贏得成功分裂的命名空間。
我的小提琴只有1個子命名空間,但可能更多。
https://jsfiddle.net/forX/kv1w2rvc/
/**************************************************************************
// FILE Master.js
***************************************************************************/
if (!Master) var Master = {};
Master.Print= function(text){
console.log("master.Print :" + text);
$("body").append("<div>master.Print : " + text + "</div>");
}
/**************************************************************************
// FILE Master.Test1.js
***************************************************************************/
if (!Master) var Master = {};
if (!Master.Test1) Master.Test1 = {};
/**************************************************************************
* Descrition :
* Function for managing event load/documentReady
**************************************************************************/
Master.Test1.onReady = function() {
$(function() {
Master.Test1.Function1(); //try to replace because need all namespace.
try {
this.Function2(); //not working
}
catch(err) {
console.log("this.Function2 not working");
$("body").append("<div>this.Function2 not working</div>");
}
try {
this.Print("onReady"); //not working
}
catch(err) {
console.log("this.Print not working");
$("body").append("<div>this.Print not working</div>");
}
try {
Print("onReady"); //not working
}
catch(err) {
console.log("Print not working");
$("body").append("<div>Print not working</div>");
}
});
}
Master.Test1.Function1 = function() {
console.log("Function1");
$("body").append("<div>Function1</div>");
this.Function3(); //working because not inside another function
}
Master.Test1.Function2 = function() {
$("body").append("<div>Function2</div>");
console.log("Function2");
}
Master.Test1.Function3 = function() {
$("body").append("<div>Function3</div>");
console.log("Function3");
Master.Print("Function3"); //try to replace because need all namespace.
}
Master.Test1.onReady();
我使用Master.Test1.Function1();我想改變它,因爲Function1在同一個命名空間內。
我使用Master.Print(「Function3」);我不認爲我可以改變這一點。我嘗試使用它的方式,更多的是繼承功能。但我不知道是否有辦法做到這一點?
也許我應該改變我的命名空間方法?也許原型會做我想要的東西?
衣櫃辦法做到這一點,你坦克:https://jsfiddle.net/forX/kv1w2rvc/1/ – forX