我试图改变某种方法来将方法调用到命名空间中。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