使用JavaScript通过循环动态创建对象

使用JavaScript通过循环动态创建对象,javascript,oop,loops,dynamic,Javascript,Oop,Loops,Dynamic,我希望有人能给我指出正确的方向 我基本上是在尝试构建一个面向对象的解决方案,以基于一个类动态生成大量对象,然后能够通过类方法运行与每个对象关联的方法 下面是我的非动态代码 // Create videoObject class function videoObject(videoTag, videoNum){ this.videoTag = videoTag; this.videoNum = videoNum; this.videoTagHref = videoTag.a

我希望有人能给我指出正确的方向

我基本上是在尝试构建一个面向对象的解决方案,以基于一个类动态生成大量对象,然后能够通过类方法运行与每个对象关联的方法

下面是我的非动态代码

// Create videoObject class
function videoObject(videoTag, videoNum){
    this.videoTag = videoTag;
    this.videoNum = videoNum;
    this.videoTagHref = videoTag.attr("href");
    this.videoId = function(videoTag){
    };
    this.addAttrId = function(videoNum){
    };
    this.printObjectInfo = function(videoId){
    };
    this.embedVideo = function(videoId, videoTag, videoNum){
    };
    this.buildControls = function(videoTag){
    };
};

// Manually create two objects and run class methods
var newVideo1 = new videoObject($('.yt-player-0'), 0);
newVideo1.videoId();
newVideo1.addAttrId();
newVideo1.embedVideo();
newVideo1.buildControls();

var newVideo2 = new videoObject($('.yt-player-1'), 1);
newVideo2.videoId();
newVideo2.addAttrId();
newVideo2.embedVideo();
newVideo2.buildControls();

// I want to somehow create newVideo1 and newVideo2 dynamically inside a loop like below
var length = $('.yt-player').length;

for (var i = 0; i < length; i++) {

}
//创建videoObject类
函数videoObject(videoTag、videoNum){
this.videoTag=videoTag;
this.videoNum=videoNum;
this.videoTagHref=videoTag.attr(“href”);
this.videoId=函数(videoTag){
};
this.addAttrId=函数(videoNum){
};
this.printObjectInfo=函数(videoId){
};
this.embeddevideo=函数(videoId、videoTag、videoNum){
};
this.buildControls=函数(videoTag){
};
};
//手动创建两个对象并运行类方法
var newVideo1=新的视频对象($('.yt-player-0'),0);
newVideo1.videoId();
newVideo1.addAttrId();
newVideo1.embeddevideo();
newVideo1.buildControls();
var newVideo2=新的视频对象($('.yt-player-1'),1);
newVideo2.videoId();
newVideo2.addAttrId();
newVideo2.embeddevideo();
newVideo2.buildControls();
//我想以某种方式在如下循环中动态创建newVideo1和newVideo2
变量长度=$('.yt player').length;
对于(变量i=0;i
如果你们能给我任何帮助,我将不胜感激

谢谢

汤姆

var视频=[2];
对于(变量i=0;i<2;i++){
视频[i]=新的视频对象($('.yt-player-0'),0);
/*这里有额外的方法调用*/
}

如果你总是想用这些额外的方法调用来初始化你的对象,你可能会考虑让你的对象构造函数处理你。那么您的客户机代码就会更干净

我会试试这个(未经测试):

//创建videoObject类
函数videoObject(videoTag、videoNum){
this.videoTag=videoTag;
this.videoNum=videoNum;
this.videoTagHref=videoTag.attr(“href”);
this.videoId=函数(videoTag){
};
this.addAttrId=函数(videoNum){
};
this.printObjectInfo=函数(videoId){
};
this.embeddevideo=函数(videoId、videoTag、videoNum){
};
this.buildControls=函数(videoTag){
};
//在构造函数中调用这些方法,而不是从其他地方重复调用
这个.videoId();
this.addAttrId();
这个.embeddevideo();
this.buildControls();
//将对新创建的视频对象的引用发送回循环
归还这个;
};
//创建视频对象引用数组
var videoObjectReferences=[];

对于(var i=0;iSo)为什么不将它们放入循环中?我的印象是,我无法动态创建这样的变量。我有点不确定从何处开始。如果需要存储对象的引用,请将其存储在数组或对象中。例如
videos['ht-player-0']=new VideoObject()
Hi Bart,谢谢你的回答。你介意详细说明一下吗,我不太确定如何实现这个。谢谢Tom。非常感谢你的帮助。我无法让这个工作,但是@itsmikem的答案很好。lol抱歉我有一些java语法。这就是为什么它可能不起作用。现在进行编辑。
var videos = [2]; 
for(var i = 0; i < 2; i++){
   videos[i] = new videoObject($('.yt-player-0'), 0);
   /*Extra method calls here*/ 
}
// Create videoObject class
function videoObject(videoTag, videoNum){
    this.videoTag = videoTag;
    this.videoNum = videoNum;
    this.videoTagHref = videoTag.attr("href");
    this.videoId = function(videoTag){
    };
    this.addAttrId = function(videoNum){
    };
    this.printObjectInfo = function(videoId){
    };
    this.embedVideo = function(videoId, videoTag, videoNum){
    };
    this.buildControls = function(videoTag){
    };
    // call these methods in your constructor instead of repeatedly from elsewhere
    this.videoId();
    this.addAttrId();
    this.embedVideo();
    this.buildControls();
    // send back a reference to this newly created video object to the loop
    return this;
};

// create an array of video object references
var videoObjectReferences = [];
for (var i=0;i<10;i++){
    // building ten video objects here, with ids of: yt-player-0, yt-player-1, etc.
    // build a selector to reference them via id, not by class with a dot as you have in your question
    var sel = String("#yt-player-" + i);
    // create the object and store a reference to the video object so you can do something with it later
    var newVid = new videoObject($(sel), i);
    // build list of references
    videoObjectReferences.push(newVid);
}