Javascript RequireJS-在同一文件中调用函数
我试图调用一个函数作为同一文件中另一个函数的回调 下面的代码没有显示错误,但不执行任何操作-是否有其他/更好的方法来执行此操作 我从require-config文件(require-main.js)调用第一个函数,效果很好Javascript RequireJS-在同一文件中调用函数,javascript,requirejs,Javascript,Requirejs,我试图调用一个函数作为同一文件中另一个函数的回调 下面的代码没有显示错误,但不执行任何操作-是否有其他/更好的方法来执行此操作 我从require-config文件(require-main.js)调用第一个函数,效果很好 define(['require-main'], function() { require(['getJSON'], function(getJSON) { getJSON.stations(); }); }); getJSON.js def
define(['require-main'], function() {
require(['getJSON'], function(getJSON) {
getJSON.stations();
});
});
getJSON.js
define(['jquery', 'domChange', 'setUp'], function($, domChange, setUp) {
var self = this;
return {
stations: function() {
$.get('/js/ajax/stations.json', function(sol) { /* local JSON */
tmv.stations = sol;
console.log(sol); /* this is fine */
self.lines; /* <-- call next function */
});
},
lines: function() {
console.log('foo'); /* NOT called */
$.get('/js/ajax/lines.json', function(lines) { /* local JSON */
/* do something */
});
}
}
});
define(['jquery','domChange','setUp'],函数($,domChange,setUp){
var self=这个;
返回{
站点:函数(){
$.get('/js/ajax/stations.json',函数(sol){/*本地json*/
tmv.stations=sol;
console.log(sol);/*这很好*/
self.lines;/*应为
tmv.stations=sol;
console.log(sol);/*这很好*/
此.lines();/*应为
tmv.stations=sol;
console.log(sol);/*这很好*/
this.lines();/*也许在调用ajax函数之前,您应该保留对this关键字的引用
stations: function() {
var self = this;
$.get('/js/ajax/stations.json', function(sol) { /* local JSON */
tmv.stations = sol;
console.log(sol); /* this is fine */
self.lines(); /* <-- call next function */
});
}
站:函数(){
var self=这个;
$.get('/js/ajax/stations.json',函数(sol){/*本地json*/
tmv.stations=sol;
console.log(sol);/*这很好*/
self.lines();/*也许在调用ajax函数之前,您应该保留对这个关键字的引用
stations: function() {
var self = this;
$.get('/js/ajax/stations.json', function(sol) { /* local JSON */
tmv.stations = sol;
console.log(sol); /* this is fine */
self.lines(); /* <-- call next function */
});
}
站:函数(){
var self=这个;
$.get('/js/ajax/stations.json',函数(sol){/*本地json*/
tmv.stations=sol;
console.log(sol);/*这很好*/
self.lines();/*对您的getJSON.js的内容尝试一下:
define(['jquery', 'domChange', 'setUp'], function($, domChange, setUp) {
var getJSON = {
stations: function() {
$.get('/js/ajax/stations.json', function(sol) { /* local JSON */
tmv.stations = sol;
console.log(sol); /* this is fine */
getJSON.lines(); /* <-- call next function */
});
},
lines: function() {
console.log('foo'); /* NOT called */
$.get('/js/ajax/lines.json', function(lines) { /* local JSON */
/* do something */
});
}
}
return getJSON;
});
define(['jquery','domChange','setUp'],函数($,domChange,setUp){
var getJSON={
站点:函数(){
$.get('/js/ajax/stations.json',函数(sol){/*本地json*/
tmv.stations=sol;
console.log(sol);/*这很好*/
getJSON.lines();/*对您的getJSON.js的内容尝试以下操作:
define(['jquery', 'domChange', 'setUp'], function($, domChange, setUp) {
var getJSON = {
stations: function() {
$.get('/js/ajax/stations.json', function(sol) { /* local JSON */
tmv.stations = sol;
console.log(sol); /* this is fine */
getJSON.lines(); /* <-- call next function */
});
},
lines: function() {
console.log('foo'); /* NOT called */
$.get('/js/ajax/lines.json', function(lines) { /* local JSON */
/* do something */
});
}
}
return getJSON;
});
define(['jquery','domChange','setUp'],函数($,domChange,setUp){
var getJSON={
站点:函数(){
$.get('/js/ajax/stations.json',函数(sol){/*本地json*/
tmv.stations=sol;
console.log(sol);/*这很好*/
getJSON.lines();/*this.lines
这是函数的引用,您没有实际调用它this.lines
这是函数的引用,您没有实际调用它,在这种情况下,它引用的是对象,而不是对象function@Francescoes.顺便说一句,他的var self=这不在同一位置,我认为nk我的解决方案应该行得通。他已经行了,无论如何,在这种情况下,这指的是对象,而不是对象function@Francescoes.顺便说一句,他的var self=这不在同一位置,我认为我的解决方案应该有效。