Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Initialize Function [duplicate]

I've recently been following some coding tutorials and I've gotten decent in my general JS knowledge (still have a ton to learn of course). On just about every tutorial I follow.. The code contains this or something like it.

    initialize: function() {
    var self = this;
    this.store = new MemoryStore(function() {
        self.renderHomeView();
    });

Specifically

    initialize: function() {
        // code in here
    }

I honestly just don't know what this is. Sure, I can finish following a tutorial just fine.. However, I want to understand what it is that I'm coding. Not just copy what someone else has written.

I've tried to use google to find out more about this, but I honestly don't have any clue what search terms to use.

Is this a way to declare a function? As in:

functionName: function(){
    // code here
}

// vs

function functionName(){
    // code here
}

What am I missing? Could someone send me a link to a resource for this?

            Thanks, Jay
like image 727
JayMartin Avatar asked Oct 17 '25 13:10

JayMartin


1 Answers

functionName: function() {
    // some code
}

This is declaring an object property called functionName with a value that's an anonymous (unnamed) function. This would only occur within curly braces defining an object literal. It's different from the following, which declares a named function in the current scope, but isn't a property of any object:

function functionName() {
    // some code
}

You can learn more about object literals here: MDN Grammar Reference

like image 156
Mark Avatar answered Oct 20 '25 04:10

Mark