2014-11-17 44 views
1

我发现这个JS代码结构和I”想知道如何调用函数移动()从函数负载内:JS - 调用一个父函数

JS

function Start(data) { 

    this.move= function() { 
     .... 
    }; 

    function load(){ 
     // call move 
    } 

} 

回答

2
function Start(data) { 
    this.move = function() { 
     .... 
    }; 

    function load(obj) { 
     obj.move(); 
    } 

    load(this); 
} 
1

功能Start()具有被实例化为一个对象。所以你会使用:

function Start(data) { 

    this.move = function() { 
    .... 
    }; 

    this.load = function(){ 
    // call move 
    this.move(); 
    } 
} 

var s = new Start(foobar); 
s.load(); 
+4

'load'是私人的,''这里面'是'窗口' –

+0

你是对的 - 已更新 –

1

这是一个JavaScript关闭。我发现this网站是有帮助的。

var move = function() { 
      alert("move"); 
     }; 

     load(); 
     function load() { 
      move(); 
     } 

此代码将只有alert Move只有一次。

4
function Start(data) { 
    var _this = this; 

    this.move = function() { 
     console.log('mode'); 
    } 

    function load() { 
     _this.move(); 
    } 

    // load(); 
} 

Start(); 
new Start(); 
1

通过使用闭包,可以通过stroing父引用来实现;

function Start(data) { 
    var me = this; 

    this.move= function() { 
     .... 
    }; 

    function load(){ 
     me.move();// call move 
    } 

} 

祝你好运。