search

Home  >  Q&A  >  body text

javascript - 请问这样的return是什么写法

我印象中记得看过某断代码,他的返回大概是

function a{
。。。。
    return{
        ...
        ...
        ...
        ...
    }
}

具体怎么写忘记了,返回中有 花括号 是什么意思呢,能不能写个例子

天蓬老师天蓬老师2897 days ago349

reply all(3)I'll reply

  • PHPz

    PHPz2017-04-10 15:18:13

    返回一个对象

    function test(){
        return {
             name:"test",
             age:23
        }
    }
    
    var obj = test();
    alert(obj.name);   //test
    

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-10 15:18:13

    有花括号代码里面是一个对象,如

    function a (){
        return {
            b: "hello,world"
        }
    }
    

    然后

    > a().b
    "hello,world"
    

    这里的b也可以是一个方法

    function a (){
    return {
        b: function(){
            console.log("hello,world")
    }}}
    

    reply
    0
  • 怪我咯

    怪我咯2017-04-10 15:18:13

    {} 定义对象字面量

    reply
    0
  • Cancelreply