2015-08-25 35 views
0

我要尋找一個JavaScript實現以下要求:的Javascript:Prototype提供接口來調用對象的方法無限

  1. 原型實現下一個運行方法。如果下一個被調用,一次迭代函數一次調用。如果運行被調用,一次被無限地調用(通過用戶請求的流產也必須是可能的,但目前沒有反映在源代碼中)。
  2. 一旦需要一個完成的參數,一旦完成就調用它。
  3. 一次迭代函數原型定義,但可以和將孩子兒童覆蓋。
  4. 無限迭代中運行必須是可行的,因此必須使堆棧丟失(因爲它發生時的功能由setTimeout的叫

我當前的問題與此源代碼:一旦家長調用不是兒童

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta charset="utf-8" /> 
    <title>JS test</title> 
    <script type="text/javascript"> 
     function write(msg) { 
     var t = document.createTextNode(msg); 
     var li = document.createElement("li"); 
     li.appendChild(t); 
     document.querySelector("ul").appendChild(li); 
     } 

     var Parent = function() { 
     var running = 0; 

     var run = function() { 
      running = Infinity; 
      invokeNextIter(); 
     }; 

     var next = function() { 
      running = 1; 
      invokeNextIter(); 
     }; 

     var invokeNextIter = function() { 
      if (running > 0) { 
      running -= 1; 
      setTimeout(function() { once(invokeNextIter); }, 1); 
      } 
     }; 

     var once = function (done) { 
      var time = Math.random() * 3 + 1; 
      write(time + " sec"); 
      setTimeout(done, time * 1000); 
     }; 

     return { 
      run: run, 
      once: once 
     }; 
     }; 

     var Child = function() { 
     var once = function (done) { 
      write("2 sec as always"); 
      setTimeout(done, 2000); 
     }; 

     var p = new Parent(); 
     return Object.create(p, { once : once }); 
     }; 

     function main() { 
     var c = new Child(); 
     c.run(); 
     } 
    </script> 
    </head> 
    <body onload="main()"> 
    <ul> 
    </ul> 
    </body> 
</html> 

無法獲得與Function.prototype.bind或該運行相關技術。任何幫助表示讚賞感激

回答

1

好了,你的情況,這是ParentChild之間不同的唯一功能是你一次()功能。

所以,你必須從你的Parent類開始,打造核心,那麼你將添加實例的方法來Parentchild,覆蓋once()功能。

我們將使用原型繼承。規則是您可以使用new關鍵字初始化任何對象的原型,並且該對象包含原生javascript對象。

function write(msg) { 
    var t = document.createTextNode(msg); 
    var li = document.createElement("li"); 
    li.appendChild(t); 
    document.querySelector("ul").appendChild(li); 
    } 

    var Parent = function(){ 
    //Retrieve context 
    var self = this; 

    var running = 0 
    //Create a `run` function which will refer to the current context 
    self.run = function(){ 
     running = Infinity; 
     invokeNextIter(); 
    } 

    //Declare our private function 
    var next = function() { 
     running = 1; 
     invokeNextIter(); 
     }; 

    var invokeNextIter = function() { 
     if (running > 0) { 
      running -= 1; 
      setTimeout(function() { 
      //Call once method which is in the prototype of the current context 
      self.once(invokeNextIter); 
      }, 1); 
     } 
     }; 
    } 

    //Add a "once" instance method to Parent 
    Parent.prototype.once = function(done){ 
    var time = Math.random() * 3 + 1; 
    write(time + " sec"); 
    setTimeout(function(){ 
     done(); 
    }, time * 1000); 
    } 


    //Create our Child 'class' 
    var Child = function(){ 

    } 

    //Declare Child as a subclass of the first 
    Child.prototype = new Parent(); 

    //Add a "once" instance method to Child and override the "once" parent method 
    Child.prototype.once = function(done){ 
    write("1.5 sec as always"); 
    setTimeout(function(){ 
     done(); 
    }, 1500); 
    } 


    function main(){ 
    // Create instances and see the overridden behavior 
    var c = new Child(); 
    var p = new Parent(); 
    //c.run() will call child once() method 
    c.run(); 
    //p.run() will call parent once() method 
    p.run(); 
    } 

在這裏,你可以看到self.run()self.once()Self變量將引用當前實例。

+0

我不能通過構造函數中的return對象提供所有的方法來運行它(但在我的例子中),但你是絕對正確的。你的方法工作並滿足我所要求的。原型繼承的方法是我顯然在尋找的。謝謝! – meisterluk

相關問題