3

I am newer to JavaScript and come from a Java background. I am trying to write a pretty simple JavaScript application but I am having some issues. I want to duplicate a sort of class structure in JavaScript the issue I am having has to do with the scope of functions inside of functions.

I want to create a structure like this

function codeArray() {
    function create() {
        addElement( prop1, prop2,) {
            //create an element
        }

        function traverse(object) {
            //recursively go through some nested list data structure
            addElement(asd, 4534);
        }
    }

    function deleteElement() {
        //delete a single element from an array
    }

    print() {
        //print array;
    }
}

something.click(function() {
    codeArray.create();
    codeArray.print();
});

So the issue I am running into is because of the scope of the functions nested inside codeArray I cannot access them from outside codeArray. Would it be bad practice of encapsulation to just get rid of the wrapper codeArray function? Can someone suggest a better re-write for how to do this?

Thanks in advance.

5
  • 2
    Easier to use a map as the Class concept, then storing the variables (properties) and functions (methods) inside as entries. There are more ways of course. Commented Apr 23, 2013 at 18:30
  • You seem to be expecting the semantics of one language to automatically transfer to another. Don't do that. They're different languages. You need to approach a new one as potentially being entirely different. Commented Apr 23, 2013 at 18:31
  • This looks more like you want to use an object literal. Commented Apr 23, 2013 at 18:31
  • @sillylittleme I am not trying to simply translate code from one language to another, I am simply trying to solve a problem. I know how I would solve the problem in Java and I was asking how to solve the problem in JavaScript. If the answer included a completely different way structure a program that would have been fine. Commented Apr 23, 2013 at 18:50
  • Well my point is that there's no reason to think the code you included would work. There's no shortage of resources dealing with object orientation in JavaScript. I'd suggest reading some tutorials. Your answer below merely creates a single object with some properties. This doesn't seem like what you were after. Commented Apr 23, 2013 at 18:53

1 Answer 1

7

You're almost right. You should be using an object instead:

codeArray = {
    create:function() {
       // ...
    },
    // ...
    print:function() {
       // ...
    }
};

Then you can call:

codeArray.create();
codeArray.print();
Sign up to request clarification or add additional context in comments.

2 Comments

This duplicates a class structure?
In common (virtual) sense; yes. Try this article for further reading; phpied.com/3-ways-to-define-a-javascript-class. Stuff like inheritance, static methods and singletons you can probably forget. Maybe you can simulate it, but it wouldn't be pretty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.