[英]How can I reuse an object literal
I'm trying to reuse an object I created to dynamically create more than one slider on a page. 我正在尝试重用我创建的对象来动态创建页面上的多个滑块。 My idea was to create an array and push my slider object there as often as needed, so I could access it by id.
我的想法是创建一个数组并根据需要经常推送我的滑块对象,所以我可以通过id访问它。 Unfortunatelly it doesn't work.
不幸的是,它不起作用。 Hope someone can point me in the right direction ...
希望有人能指出我正确的方向......
So what I have is this; 所以我拥有的就是这个;
var slider = {
"init":function(slide_it){
this.parent = $(slide_it);
/Count Elements and create a navigation depending on the count etc./
},
"otherstuff":{...}
}
In my (document).ready function I create an array and fill it up with different slider objects, add Ids to an accordion and call the init function: 在我的(文档).ready函数中,我创建一个数组并用不同的滑块对象填充它,将Ids添加到手风琴中并调用init函数:
var slide_array = [];
var accordion_sections = $('#accordion > div').length;
for(var i = 0; i < accordion_sections; i++){
slide_array.push(slider);
$('#accordion').children('div').eq(i).attr('id', 'slide_it_'+ i);
slide_array[i].init($('#slide_it_' + i).find('.slider'));
}
Then I have a button with class="next" and I call a function within the slider 然后我有一个class =“next”的按钮,我在滑块内调用一个函数
$('.next').click(function(){
slide_array[0].otherstuff();
});
My plan is to get the parent of .next and its id so that I can use slide_array[parentID].otherstuff();
我的计划是获取.next的父节点及其id,以便我可以使用
slide_array[parentID].otherstuff();
But ... it's not working propperly when I call the init function inside the for loop more then once. 但是......当我在for循环中多次调用init函数时,它没有正常工作。
More weird, some functions calls seem to work, other have no effect. 更奇怪的是,一些函数调用似乎有效,其他函数调用没有效果。 What am I doing wrong?
我究竟做错了什么?
You can use Object.create
. 您可以使用
Object.create
。
var s1 = Object.create(slider),
s2 = Object.create(slider);
s1.init(...);
s2.init(...);
If you return this
from init
your will be able to chain like: 如果你从
init
返回this
,你将能够链接:
var s1 = Object.create(slider).init(...);
However at this point I would just ditch the object literal and use constructors, since this is what you need. 但是在这一点上,我会抛弃对象文字并使用构造函数,因为这是你需要的。
function Slider(slide_it) {
this.parent = $(slide_it);
}
Slider.prototype = {
constructor: Slider,
otherStuff: function () {}
};
var s1 = new Slider(...),
s2 = new Slider(...);
Write a function to return the object: 编写一个返回对象的函数:
function slider() {
return {
"init":function(slide_it){
this.parent = $(slide_it);
/Count Elements and create a navigation depending on the count etc./
},
"otherstuff":{...}
};
}
Then: 然后:
slide_array.push( slider() );
That'll give you a separate object every time. 那每次都会给你一个单独的对象。 In you're version, you're filling the array with references to the same single object.
在您的版本中,您将使用对同一个对象的引用填充数组。
Why not just turn that into a jQuery plugin ? 为什么不把它变成一个jQuery插件 ?
jQuery.fn.slider = function(options) {
return this.each(function() {
var sliderElem = $(this),
settings = $.extend({
speed : 3000,
something : 'other thing'
}, options);
otherStuff(sliderElem);
});
function otherStuff(elem) {
}
}
$('#accordion > div').slider();
No iteration or jumping through hoops, just call it on the collection and it creates a new slider for each element ? 没有迭代或跳过箍,只是在集合上调用它,它为每个元素创建一个新的滑块?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.