-3

This may be asked several times, but I need it ASAP for a production-environment and I am totally overwhelmed by which objects I could create and use.

function scroll(min, max) {
  // do stuff
}

function scrollmore() {
  min += 10;
  max += 10;
  // do more stuff
}

I will accept an answer which guides me on the right path through a link or an explicit answer (which works). ;)

loveNoHate
  • 1,549
  • 13
  • 21
  • @Omnicide Thanks, tried to be generic to make more use for future visitors, but hey, look at them votes!! :o – loveNoHate Dec 06 '13 at 11:15
  • So at the top you say to inscroller, but at the bottom it's from? As in you're looking to pass back the pic and img? I guess I'm still not 100% clear on the question – Inversus Dec 06 '13 at 11:26
  • @Omnicide I heard you can use an object to make a value available outside of a function. That's what I ask. How to do that? – loveNoHate Dec 06 '13 at 11:29
  • @Omnicide I want to use the same function, but with updated argument values on each call. Specifically said. Generically said: How can I pass a value from a function using OOP? ;) – loveNoHate Dec 06 '13 at 11:35
  • @Grundy Could you please have a look again, perhaps it is now more clear? Thank you. :) – loveNoHate Dec 06 '13 at 11:48
  • @matteo-tassinari Since you guys placed this on hold, the asker has clarified and I've answered. Can this be taken off hold? – Inversus Dec 06 '13 at 12:34
  • @rory-mccrossan Since you guys placed this on hold, the asker has clarified and I've answered. Can this be taken off hold? – Inversus Dec 06 '13 at 12:34
  • @jai Since you guys placed this on hold, the asker has clarified and I've answered. Can this be taken off hold? – Inversus Dec 06 '13 at 12:35
  • @melanciauk Since you guys placed this on hold, the asker has clarified and I've answered. Can this be taken off hold? – Inversus Dec 06 '13 at 12:35
  • @bart Since you guys placed this on hold, the asker has clarified and I've answered. Can this be taken off hold? – Inversus Dec 06 '13 at 12:36

1 Answers1

1

One way to take an OOP approach to this might be something like this. The min and max values are members of the object stored in objVar. This is very much like the object representing the min and max values you were looking for. Why not take it a step or two further, though?

We can include the inScroller() function. Now, since it's a part of the object, it can access objVar.min and objVar.max directly and doesn't need to have them passed to it. You can just call objVar.inScroller(); and it will operate on the variables objVar.min and objVar.max.

So what about incrementing by 10? Extending that same concept, we can create a function that increments the objVar.min and objVar.max variables and simply call it thusly: objVar.increment();.

Next time that objVar.inScroller(); is called, objVar.min and objVar.max will have been incremented by 10 each. You could even put 'em in a loop, like:

for (int i=0;i<totalRuns;i++) { objVar.inScroller(); objVar.increment(); }

The Object:

var objVar = new function() {
    this.min = 0;
    this.max = 10;
    this.increment() = function() { min += 10; max += 10; }


    this.inScroller = function() {
        for (var i=min;i<max;i++) {
            var pic = $('.scrolling p')[i],
            pic = $(pic).text(),
            var img = $('<img />').attr("src","img/profile/" + pic).css('display','none');
            $('.pfiles').append(img);
            $('img').load(function() {
                $(this).fadeIn(400)
            });
    }
}

Finally, it's called thusly:

$(window).scroll(function() {
    if ($(window).scrollTop() + window.innerHeight >= $('.pfiles').height() && $('.scrolling p').length > $('.pfiles img').length) {
        objVar.increment();
        objVar.inScroller();
    }
});

Or even thusly:

$(window).scroll(function() {
    if ($(window).scrollTop() + window.innerHeight >= $('.pfiles').height() && $('.scrolling p').length > $('.pfiles img').length) {
        for (int i=0;i<totalRuns;i++) { objVar.inScroller(); objVar.increment(); }
    }
});
user229044
  • 232,980
  • 40
  • 330
  • 338
Inversus
  • 3,125
  • 4
  • 32
  • 37
  • Did I miss something or is this just a random answer? – Bart Dec 06 '13 at 11:06
  • Nope, just a random answer. – Inversus Dec 06 '13 at 11:07
  • It's impossible to know what he's asking :) – Bart Dec 06 '13 at 11:08
  • I do not know how to pass that value, I could use a global variable of course, but yesterday I read so nice about objects and thought I understood it and could use it here. So, what is an advanced way to make a value globally available? Thanks!! – loveNoHate Dec 06 '13 at 11:21
  • @Bart Could you please have a look again? Perhaps you can help me now!?! Cheers! – loveNoHate Dec 06 '13 at 11:45
  • But how can I get the value to the next function? This is what I am stuck, the rest runs fine. ;/ – loveNoHate Dec 06 '13 at 11:50
  • What next function do you mean? Also, I updated the answer again to include how to use the object – Inversus Dec 06 '13 at 11:52
  • Edited to reflect the fact that you need an array for this.img – Inversus Dec 06 '13 at 11:55
  • Ayyyy, that's nice from you making such an effort. But look at my "answer" (where is the answer button for me?!?). Perhaps you can tell me how to do the difference between both codes with OOP? Thanks. Thanks a lot! – loveNoHate Dec 06 '13 at 11:59
  • Yeah!! You did it!! I got the question updated with your code, it is working! ;) THANKS!!!! – loveNoHate Dec 06 '13 at 12:11
  • No problem. Check it out now, actually. This version you'll really like – Inversus Dec 06 '13 at 12:20
  • @dollarvar If you haven't checked it out yet, I'd suggest it. This is probably the way to go as it treats the whole thing as an object. No messing around with values and whatnot. Anyways, happy to help! :) – Inversus Dec 06 '13 at 12:26
  • That looks really cool, thank you! I do not know yet though, how to pass the values from `objVar.increment()` into `objVar.inscroller()`? Below `.scroll()` I mean? ;) And thanks, cool move in notifying the guys who put that on hold, puh, that was a hard task from a generic question to this piece here. ;) – loveNoHate Dec 06 '13 at 12:47
  • It so often is! It's a really important task, though. I'm happy to have helped with it. :) --- I left another edit. There's the cool thing: you don't have to pass them at all! – Inversus Dec 06 '13 at 13:19
  • Why do I keep getting negative votes for this?? A guy had a question and I gave an answer (which he found useful and accepted). I get a +1 every now and then so I must have a total of 10 or 15 negative votes. Why the hate and reputation damage you guys? – Inversus Feb 28 '14 at 11:29
  • Hey, Inversus, we both changed names ;) Ehm, about the downvotes, this must be a loomed thread, remember the downvote spree on me. ;) 1 – loveNoHate May 16 '14 at 16:54
  • @DOCASAREL Oh hello again. Indeed I did. Omnicyde was my gamertag, but it's kinda.. negative lol. It's like regicide means killing a king, infanticide means killing an infant, genocide means killing a whole race of people, omnicide means killing everyone!! MWahahah. So, ya. Changed it. lol. – Inversus May 27 '14 at 13:49
  • @DOCASAREL I do remember that. The downvoters can be so harsh. It discourages people from asking questions, IMHO. Good to see that your number's almost in the +ve from the question-cide (lol). BTW, what do you mean by 'loomed thread'? – Inversus May 27 '14 at 13:51
  • Loomed, was just a word I intuitively got into my German mind, and it is somehow crazy, all these downvotes for serious questions and a, pfff, top answer!?! Look at that: http://stackoverflow.com/a/3638034/2672018. This was what I was looking for. I call it the [SHOG9](http://stackoverflow.com/users/811/shog9) method now, use it all the time. ;) BTW: It is interesting, I used to call myself DOC AZRAEL. Google that last part up. HAHA, connected in mind. ;) – loveNoHate May 28 '14 at 05:02