2

I'm going through a javascript file having some functions like this:

'form_validation': function(form,error_bin){
        for(var field in form){
            if(field.substr(0,1) != "$"){
                this.validation_messages(field,form,error_bin);
            }
        }
    }

I want to understand what is the difference between defining a function with quotes (like mentioned above) and without quotes

EDIT: I've also observed that the functions having name in quotes are being called from a different file (like: ServiceName.functionName()), while without quotes are being called from the same file. This is an Angularjs code

3 Answers 3

5

The two are equivalent syntax for object literals. The quotes allow you to use keys that aren't legal variable names, like so:

    var foo ={ 'variable name':2}

There's nothing significant about the fact that the value assigned to the object key is a function. In JavaScript objects are (almost) just key value pairs of strings and arbitrary objects.

Sign up to request clarification or add additional context in comments.

Comments

1

JavaScript has something called Objects these are link associative array's in other languages. Object have item. Each item has a key(name) and a value. Key's follow regular JavaScript variable names. You can check valid variable names here. Valid variable names do not need to be in quotes. Sometimes, to include special characters, the name is a string.

my_value  <- Valid
my-value  <- Invalid (needs to be in string)
my value  <- Invalid (needs to be in string)

Comments

0

both are same for javascript objects. but you should avoid using quotes as a coding standard.

Comments

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.