我尝试改变一些方法将方法调用到命名空间。
我将文件中的每个命名空间分开(希望保持这种方式)
我尝试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”);我不认为我可以改变这一点。我尝试使用它的方式,它更像是一个继承函数。但我不知道是否有办法做到这一点?
也许我应该更改我的命名空间方法?也许原型会做我想做的事情?
答案 0 :(得分:1)
您可以捕获变量中的this
,因为this
内的$(function() {})
将指向document
个对象。如果您从未更改onReady
的调用上下文,则以下内容将起作用 - 即它始终在Test1
对象上调用,而不在其他上下文中调用:
Master.Test1.onReady = function () {
var self = this;
$(function () {
self.Function1();
// ..
});
}
要访问Print
,您必须使用Master
对象进行引用,例如:Master.Print()
,因为它在Test1
对象中无法使用
答案 1 :(得分:0)
this
document
.ready()
jQuery()
.ready()
function(){}
$(function() {})
,其中this
是参数this.Function2()
。 document
$('#pagination').pagination({
dataSource: function(done) {
var result = [];
for (var i = 1; i <= 1000; i++) {
result.push(i);
}
done(result);
},
pageSize: 10,
callback: function(data, pagination) {
console.log(data[0]);
}
});
会引用<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/paginationjs/2.0.8/pagination.min.js"></script>
<div id="pagination"></div>
。
答案 2 :(得分:0)
如果您想拥有任何内部上下文,则需要创建“对象原型”的“实例”。此时,您可以在其他功能中使用“this.otherFunction”。这是一个小例子:
var MyObject = function() {};
MyObject.functionOne = function() {
console.log("Function 1");
this.functionTwo();
};
MyObject.functionTwo = function() {
console.log("Function 2");
};
var instanceOne = new MyObject();
instanceOne.functionOne();
您可能会获得有关对象定义here
的更多信息