什么是J" get"函数出现在对象内?

时间:2015-12-09 00:16:04

标签: javascript

我遇到这段代码并且我不熟悉语法 - " get:"代表?为什么这样写呢?

var newVar = {

        get: function fn() {

            return this.val;
        },
        val: 43
    };

    var child = Object.create(newVar);
    child.val = 333;

    var child2 = Object.create(child);



    console.log(child.get() + child2.get());

1 个答案:

答案 0 :(得分:2)

get没有什么特别之处。在您的示例中,它只是名为get的对象的属性。通过调用getMeOutOfHere

可以轻松实现
var newVar = {

  getMeOutOfHere: function () { console.log('we\'re leaving'); }

}