JavaScript Classes

前端 未结 8 467
心在旅途
心在旅途 2020-12-07 09:21

I understand basic JavaScript pseudo-classes:

function Foo(bar) {
    this._bar = bar;
}

Foo.prototype.getBar = function() {
    return this._bar;
};

var f         


        
8条回答
  •  不思量自难忘°
    2020-12-07 10:07

    This closure allows instantiation and encapsulation but no inheritance.

    function Foo(){
        var _bar = "foo";
    
        return {
            getBar: function() {
                return _bar;
            },
            setBar: function(bar) {
                _bar = bar;
            }
        };
    };
    
    a = Foo();
    b = Foo();
    
    a.setBar("bar");
    alert(a.getBar()); // "bar"
    alert(b.getBar()); // "foo"
    

提交回复
热议问题