Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

So I have the following piece of code:

var structures = {

    loginStructure : function(){

        return structure = [
            '<form name="',opts.formClass,'" class="',opts.formClass,'" method="post" action="#">',
                '<fieldset class="',opts.fieldsWrapper,'">',
                    '<fieldset class="',opts.userWrapper,'">',
                        '<label for="',opts.userInt,'" class="',opts.userLbl,'"><img src="',opts.userIcon,'" alt="',opts.userName,'" /></label>',
                        '<input type="text" name="',opts.userInt,'" class="',opts.userInt,'" placeholder="',checkNameLenght(opts.userName,namesLenght.userNameLenght,16,'Username'),'" value="" autocomplete="off" />',
                    '</fieldset>',
                    '<fieldset class="',opts.passWrapper,'">',
                        '<label for="',opts.passInt,'" class="',opts.passLbl,'"><img src="',opts.passIcon,'" alt="',opts.passName,'" /></label>',
                        '<input type="password" name="',opts.passInt,'" class="',opts.passInt,'" placeholder="',checkNameLenght(opts.passName,namesLenght.passNameLenght,16,'Password'),'" value="" autocomplete="off" />',
                    '</fieldset>',
                    '<fieldset class="',opts.btnWrapper,'">',
                        '<button type="submit" name="',opts.btnInt,'" class="',opts.btnInt,'">',checkNameLenght(opts.btnName,namesLenght.btnNameLenght,7,'Login'),'</button>',
                    '</fieldset>',
                '</fieldset>',
                '<div class="toogle-button">',
                    '<ul class="inside">',
                        '<li class="toogle"><a><img src="assets/gfx/toogle.png" alt="Back" /></a></li>',
                    '</ul>',
                '</div>',
            '</form>',
            '<div class="toogle-buttons">',
            '</div>'
        ];
    }

}

This returns ( if I do console.log(structures.loginStructure) ) just a function(). Is there a way I can make it to return the actual array I have in there ?

The purpose of it would be to have multiple arrays defined as object keys, if it is possible to return such a thing, because it seems easier. Or is there a better way to do this ?

share|improve this question
    
BTW, this has nothing to do with jQuery - but leaving it tagged as such as an indication that you're probably using jQuery - which may act as a hint to someone else to provide a jQuery-dependent answer. –  ziesemer Jan 14 '12 at 20:19
    
Yes, I'm using jQuery too, in fact this piece of code is part of a jQuery plugin. –  rolandjitsu Jan 14 '12 at 20:25
    
Seems to work fine here if all variables are defined properly: jsfiddle.net/jfriend00/ekZVE. –  jfriend00 Jan 14 '12 at 20:53
    
@jfriend00 indeed it does. I have the following structure of the jQuery plugin: jsfiddle.net/3fbZq/1 , and the opts are defined within that $.fn.loginsys.defaults = { ... } . And if I run that console.log() operation nothing shows up in this current state. If I had that object return a function as the first part of the answer says it shows as I described function() in the console. So maybe something is wrong with my structure of the plugin and that is why it's not working properly. If you don't mind you could take a look jsfiddle.net/3fbZq/1 and tell me what's wrong. –  rolandjitsu Jan 14 '12 at 21:26
    
@Roland - I made your fiddle so it will actually run and execute and when I do that, it gives errors saying that the namesLenght object is not defined and thus it aborts. Also, do you realize that the normal spelling is namesLength not namesLenght. You can see the runnable version and the error here: jsfiddle.net/jfriend00/rdbfU. –  jfriend00 Jan 14 '12 at 21:35

1 Answer 1

up vote 4 down vote accepted

You want:

structures.loginStructure();

structures.loginStructure is simply just returning a reference to the function, instead of executing the function and returning its result. Add the () to the end of it to execute it and return its result.

Alternatively, and maybe better, don't write it as a function. Just declare loginStructure: ['<form name.... Basically, just remove function(){return structure =. A significant difference to note here is that any values of opts would be evaluated immediately, instead of deferred until later when then function would be executed - so please don't blindly update your code to this.

share|improve this answer
    
And instead of ',opts.formClass,' having '+opts.formClass+' ? –  rolandjitsu Jan 14 '12 at 20:21
    
I tried but then it gives me an error down on my code on which line I have return this.each(function() { ....... , why ? –  rolandjitsu Jan 14 '12 at 20:24
    
@Roland - I think we'd need to see more code to accurately answer that, which I think may be appropriate for a new question. –  ziesemer Jan 14 '12 at 20:27
    
Sorry, my fault here, I forgot to delete } from the end of the funtion(){ ... I had there before. It's fine now, but now nothing happens if I do this console.log(structures.loginStructure), I'm suppose to see that array, no ? –  rolandjitsu Jan 14 '12 at 20:30
1  
This answer no longer related to the current state of the question (after it was edited). –  jfriend00 Jan 14 '12 at 20:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.