Avatar of oggiemc
oggiemc
Flag for Ireland asked on

Javascript self invocation and function properties

Hello all,
Im trying to understsnd the attached code..Can someone explain to me whats going on..I understand the syntax of selfinvoking functions and how they are automatically called when defined but i have the following questions regarding this code:

(1) I assume the first function to be called is next() : line 20.
(2) currentIndex = (currentIndex + 1) % foundElements.length;  // What is the meaning of this line? i understand what its doing, but why is it doing it??
(3) Semicolon on line 21 a typo?
(4) Line 23, i assume the search here refers to the outer self invoking function and not the inner search function?
(5) Line 26, same question as (4). How do i access search.results and search.currentIndex from outside the self invoking function??

(6) Line 29, what exactly is being returned?
(7) What is the order of execution in this code? line by line
(8) If i wanted to pass in a value to the function i assume i would do the following modifications:
Line 30 : })(input);
Line 1 : var search = (function (tagName) {

Thanks
var search = (function () {
    var foundElements = [],
        lastTagName, currentIndex = -1;

    function next() {
        if (foundElements.length === 0) {
            return null;
        }
        alert('here');
        currentIndex = (currentIndex + 1) % foundElements.length;
        return foundElements[currentIndex];
    }

    function search(tagName) {
        if (tagName && tagName !== lastTagName) {
            alert('tagname');
            foundElements = documentNode.getElementsByTagName(tagName);
            lastTagName = tagName, currentIndex = -1;
        }
        return next();
    };

    search.results = function () {
        return foundElements.slice();
    };
    search.currentIndex = function () {
        return currentIndex;
    };
    return search;
})();

Open in new window

JavaScript

Avatar of undefined
Last Comment
Michel Plungjan

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Rob

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Rob

Here is a working example:

http://jsfiddle.net/rjurd/Lgqvs/
Michel Plungjan

The semi colon is not needed unless the code is minified
Rob

that's not right... the semicolon isn't needed even if it is minified
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Michel Plungjan

So it is not needed.

(3) Semicolon on line 21 a typo?
NO It is assigning the function to the parent object
Rob

Sorry I misunderstood what you were referring to. Eg the other two functions that do not have semicolons. They still do not require them if the code is minified.
In referring to the authors question about line 21 typo this IS required
Michel Plungjan

because there is a non-function statement after it?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.