简体   繁体   English

JavaScript成员函数声明

[英]Javascript member function declaration

I have the following object 'obj'. 我有以下对象'obj'。 I tried to debug it and seems like both functions are members 'render' and 'sort'. 我试图调试它,似乎两个函数都是成员“ render”和“ sort”。 Is there any difference between these two types of declaration? 这两种声明之间有什么区别吗?

var obj = {
    render:function(){

    },
    sort(){

    }
}

You're using new object literal notation introduced in ES6. 您正在使用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. 如果您对新的Javascript功能感兴趣,则可以考虑研究诸如traceurbabel之类的ES6 编译器 ,以便获得跨浏览器的支持。

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. 看到像这样在对象属性上声明的命名函数是不寻常的,因为它实际上是ES6的新语法,在许多当前的浏览器中都无法使用。 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? 很好,不是吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM