Javascript 使用循环函数简化代码

Javascript 使用循环函数简化代码,javascript,cycle,simplify,Javascript,Cycle,Simplify,我有多个使用相同循环代码的函数,我想知道有没有可能通过使用一个循环函数来简化代码,这样我就可以通过调用想要的函数名来执行代码 现在: for(var i=0;i不使用名称空间: window["functionName"](arguments); 所以,把它包起来,这样使用它: cycle(someFunction); function cycle(name){ for(var i=0;i<all;i++){ window[name](i);; } } 没

我有多个使用相同循环代码的函数,我想知道有没有可能通过使用一个循环函数来简化代码,这样我就可以通过调用想要的函数名来执行代码

现在:


for(var i=0;i不使用名称空间:

window["functionName"](arguments);
所以,把它包起来,这样使用它:

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

没有名称空间使用:

window["functionName"](arguments);
所以,把它包起来,这样使用它:

cycle(someFunction);
function cycle(name){
    for(var i=0;i<all;i++){
       window[name](i);;
    }
}
从字面上看尝试访问以
lineGroups.lines[0]
开头的属性。只有在显式执行
window['lineGroups.lines[0]]]=…
的情况下,此类属性才会存在,我确信您没有执行此操作

根本不需要涉及
窗口
。只需访问对象的
属性:

this.lines[i][callFunction]();
我没有得到任何错误,但函数没有执行

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");
    }
}
访问不存在的属性不会产生错误。例如:

window[';dghfodstf0ap9sdufgpas9df']
从字面上看尝试访问以
lineGroups.lines[0]
开头的属性。只有在显式执行
window['lineGroups.lines[0]]]=…
的情况下,此类属性才会存在,我确信您没有执行此操作

根本不需要涉及
窗口
。只需访问对象的
属性:

this.lines[i][callFunction]();
我没有得到任何错误,但函数没有执行

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");
    }
}
访问不存在的属性不会产生错误。例如:


window[';dghfodstf0ap9sdufgpas9df']
请注意,这可能有点过分,但使用函数来创建类对象(您可以通过谷歌搜索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();
//makeClass-由Hubert Kauker(麻省理工学院授权)
//原件由John Resig(麻省理工学院授权)。
函数makeClass(){
内部变量;
返回函数(args){
if(此实例为arguments.callee){
if(typeof this.init==“函数”){
this.init.apply(this,isInternal?args:参数);
}
}否则{
isInternal=真;
var实例=新参数。被调用方(参数);
isInternal=false;
返回实例;
}
};
}
变量行=函数(){
this.addSpeed=函数(){
控制台日志(“工作”);
};
};
var LineGroup=makeClass();
LineGroup.prototype.init=函数(行号){
this.lAmount=行号?行号:5,
this.lines=[],
this.createLines=函数(虚构){
console.log(神话);
var i=0;
for(;i
请注意,这可能有点过分,但使用函数创建类对象(您可以在谷歌上搜索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();
//makeClass-由Hubert Kauker(麻省理工学院授权)
//原件由John Resig(麻省理工学院授权)。
函数makeClass(){
内部变量;
返回函数(args){
if(此实例为arguments.callee){
if(typeof this.init==“函数”){
this.init.apply(this,isInternal?args:参数);
}
}否则{
isInternal=真;
var实例=新参数。被调用方(参数);
isInternal=false;
返回实例;
}
};
}
变量行=函数(){
this.addSpeed=函数(){
控制台日志(“工作”);
};
};
var LineGroup=makeClass();
LineGroup.prototype.init=函数(行号){
this.lAmount=行号?行号:5,
this.lines=[],
this.createLines=函数(虚构){
console.log(神话);
var i=0;
for(;i
像符咒一样工作;)像符咒一样工作;)