andrey
andrey

Reputation: 1977

Javascript member function declaration

I have the following object 'obj'. I tried to debug it and seems like both functions are members 'render' and 'sort'. Is there any difference between these two types of declaration?

var obj = {
    render:function(){

    },
    sort(){

    }
}

Upvotes: 0

Views: 43

Answers (2)

kieranpotts
kieranpotts

Reputation: 1606

The first is an anonymous function expression. The second is a named function declaration. It is unusual to see named functions declared on object properties like this, because it's actually new ES6 syntax and won't work in many current browsers. For this reason it is not recommended that the second syntax be used for another year or two at least! It's nice though, isn't it?

Upvotes: 1

spender
spender

Reputation: 120480

You're using new object literal notation introduced in ES6. It's perfectly valid, but won't work in older browsers and some current ones. If new Javascript features are of interest, you might consider looking into an ES6 transpiler such as traceur or babel so that you get cross-browser support.

Upvotes: 2

Related Questions