Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript类中创建方法事件_Javascript_Jquery_Class - Fatal编程技术网

在javascript类中创建方法事件

在javascript类中创建方法事件,javascript,jquery,class,Javascript,Jquery,Class,在wapp.js中,我有以下JavaScript类: function Wapp() { this.page = function($page_name) { this.onLoad = function($response) { } } this.navigate = { changePage: function(link) { // Ajax request to load the page

在wapp.js中,我有以下JavaScript类:

function Wapp() {
    this.page = function($page_name) {
        this.onLoad = function($response) {

        }
    }
    this.navigate = {
        changePage: function(link) {
            // Ajax request to load the page
            $.post(link, {}, function(response) {
                // Code to display the page
            });
        }
    }
}
在app.js脚本中,我有以下代码:

var wapp = new Wapp();
我想这样做:

wapp.page('home').onLoad(function() {
    // doSomething();
}

// When I call the method 'changePage'
wapp.navigate.changePage('home');

// at the end of the page load 'home' I want to call the function 'doSomething()' inside the 'onLoad' method

我应该如何定义类中的方法以确保在某个操作结束时(在本例中是在ajax调用结束时)运行app.js中“onLoad”方法中定义的代码?

在构建类时,您能够将函数作为变量分配给类。您可以稍后在代码中调用分配的函数(例如,在其他函数的末尾)

function func (functionToCall) {
  this.functionToCall = functionToCall;

  this.doSomething = function () {
    this.functionToCall();
  }
}

var f = new func (
  function() {
    alert('this works');
  }
);

f.doSomething();