2015-08-25 33 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

相关问题