2015-11-03 70 views
0

我有多個使用相同循環代碼的函數,我想知道是否有可能通過使用一個循環函數來簡化代碼,所以我可以通過調用想要的函數名稱來執行代碼。使用循環功能簡化代碼

現在:

for(var i=0;i<all;i++){ someFunction(i) } 

極品:

cycle(someFunction); 
function cycle(name){ 
    for(var i=0;i<all;i++){ 
     name(i); 
    } 
} 

我試圖通過使用「窗口」要做到這一點,我沒有錯誤,但不會執行該功能。

var MyLines = new lineGroup(); 
MyLines.createLines(); // works 
MyLines.addSpeed(); // doesn't work 


var lineGroup = function(){ 
    this.lAmount = 5, 
    this.lines = [], 

    this.createLines = function(){ 
     for(var i=0,all=this.lAmount;i<all;i++){ 
      this.lines[i] = new line(); 
     } 
    }, 

    this.addSpeed = function(){ 
     // no error, but it's not executing addSpeed function 
     // if i write here a normal cycle like in createLines function 
     // it's working ok 
     this.linesCycle("addSpeed"); 
    }, 
    this.linesCycle = function(callFunction){ 
     for(var i=0,all=this.lAmount;i<all;i++){ 
      window['lineGroup.lines['+i+'].'+callFunction+'()']; 
     } 
    } 
} 

var line = function(){ 
    this.addSpeed = function(){ 
     console.log("works"); 
    } 
} 

回答

1
window['lineGroup.lines['+i+'].'+callFunction+'()']; 

字面上試圖訪問以lineGroups.lines[0]開頭的屬性。這樣的財產只會存在,如果你明確做window['lineGroups.lines[0]'] = ...,我相信你沒有。

完全沒有必要涉及window。剛剛訪問對象的屬性line

this.lines[i][callFunction](); 

我沒有錯誤,但不會執行該功能。

訪問不存在的屬性不會產生錯誤。例如:

window[';dghfodstf0ap9sdufgpas9df']

這將嘗試訪問屬性;dghfodstf0ap9sdufgpas9df,但由於它不存在,這將導致undefined。由於返回值沒有做任何事情,所以沒有觀察到變化。

+0

工程就像一個魅力;) – wyy

1

如果沒有名稱空間的使用:

window["functionName"](arguments); 

SO包起來,因此使用它:

cycle(someFunction); 
function cycle(name){ 
    for(var i=0;i<all;i++){ 
     window[name](i);; 
    } 
} 

與命名空間,包括:

window["Namespace"]["myfunction"](i); 
1

請注意,這可能有點矯枉過正,但使用函數來創建一個類對象(您可以谷歌makeClass和它爲什麼/可能是有用的),你可以創建對象的實例。

// makeClass - By Hubert Kauker (MIT Licensed) 
// original by John Resig (MIT Licensed). 
function makeClass() { 
    var isInternal; 
    return function (args) { 
     if (this instanceof arguments.callee) { 
      if (typeof this.init == "function") { 
       this.init.apply(this, isInternal ? args : arguments); 
      } 
     } else { 
      isInternal = true; 
      var instance = new arguments.callee(arguments); 
      isInternal = false; 
      return instance; 
     } 
    }; 
} 
var line = function() { 
    this.addSpeed = function() { 
     console.log("works"); 
    }; 
}; 
var LineGroup = makeClass(); 

LineGroup.prototype.init = function (lineNumber) { 
    this.lAmount = lineNumber?lineNumber:5, 
    this.lines = [], 

    this.createLines = function (mything) { 
     console.log(mything); 
     var i = 0; 
     for (; i < this.lAmount; i++) { 
      this.lines[i] = new line(); 
     } 
    }, 

    this.addSpeed = function() { 
     console.log("here"); 
     this.linesCycle("addSpeed"); 
    }, 
    this.linesCycle = function (callFunction) { 
     console.log("called:" + callFunction); 
     var i = 0; 
     for (; i < this.lAmount; i++) { 
      this.lines[i][callFunction](); 
     } 
    }; 
}; 
var myLines = LineGroup(); 
myLines.createLines("createlines"); 
myLines.addSpeed(); 
//now add a new instance with 3 "lines" 
var newLines = LineGroup(3); 
newLines.createLines("createlines2") 
console.log("addspeed is a:" + typeof newLines.addSpeed); 
console.log("line count"+newLines.lAmount); 
newLines.addSpeed();