访问对象中的函数

时间:2018-11-12 19:15:19

标签: javascript object

我尝试使用console.log在我的对象中访问此功能,但我不太明白为什么我无法访问它! 我开始使用Javascript,但是我真的很想访问Object中的函数。

感谢您的帮助

const hotel = {
    name: "Grand Hotel",
    location: "Stockholm",
    pricePerNight: 2200,
    roomBooked: 23,
    totalRoom: 223, 
    roomAvailable: function(){
        return this.totalRoom - this.roomBooked;
        
    }   
    };
    hotel.roomAvailable();
    console.log(hotel.roomAvailable);

3 个答案:

答案 0 :(得分:2)

您只是在log函数中缺少括号:

POST /events/_search
{
    "size": 0,
    "aggs": {
        "grouped_by_id": {
            "terms": {
                "field": "id.keyword",
                "size": 100,
                "order": {"max_seq":"desc"}
            },
            "aggs": {
                "max_seq": {"max":{"field":"sequence"}},
                "latest_event": {
                    "top_hits": {
                        "sort": [{"sequence":{"order":"desc"}}],
                        "from": 0,
                        "size": 1
                    }
                },
                "first_event": {
                    "top_hits": {
                        "sort": [{"sequence":{"order":"asc"}}],
                        "from": 0,
                        "size": 1
                    }
                },
                "filters": {
                    "filter": {"bool":{"must":[{"match":{"status":"READY"}}]}},
                    "aggs": {
                        "latest_ready_seq": {"max":{"field":"sequence"}}
                    }
                },
                "should_we_consider": {
                    "bucket_selector": {
                        "buckets_path": {
                            "latest_seq": "max_seq",
                            "latest_ready_seq": "filters>latest_ready_seq"
                        },
                        "script": "params.latest_seq == params.latest_ready_seq"
                    }
                }
            }
        }
    }
}

答案 1 :(得分:0)

您已经在调用该函数,它将返回该值。只需记录返回值即可。

在您的代码中,您只是在记录函数的定义,而没有真正调用它。

const hotel = {
  name: "Grand Hotel",
  location: "Stockholm",
  pricePerNight: 2200,
  roomBooked: 23,
  totalRoom: 223,
  roomAvailable: function() {
    return this.totalRoom - this.roomBooked;

  }
};

var isRoomAvailable = hotel.roomAvailable();
console.log(isRoomAvailable);

答案 2 :(得分:0)

这是您想要的吗?

@NodeEntity
public class MyClass{
  ...
  @Getter
  @GraphId
  private Long id;

  ....
}