"constructor" that are not intended to be used with a constructor call

(full code in this github repo )

In you do not know the series of JS booklets (in particular, the name of this & Object Prototypes), as well as in many SO answers (for example, this one ), the point is often made that there is no such thing as “constructor functions”, but rather ordinary functions, called by the "constructor call". I am trying to understand this point by creating vanilla functions that are not meant to be called using newto create my objects.

The first attempt works:

var assert = function(condition, message) {
    if (!condition)
        throw new Error(message||'assertion error');
};

var Counter1 = function() {
    var count = 0;
    return {get: function() {return count;},
            inc: function() {count++;}};
};

var c2a = Counter1();
c2a.inc();
assert(c2a.get()===1);
var c2b = Counter1();
assert(c2b.get()===0);
assert(c2a.get()===1); // previous counter is a separate object

, getter/setter ( ):

var Counter2 = function Counter2_() {
    var count = 0;
    Counter2_.prototype.get = function() {return count;};
    Counter2_.prototype.inc = function() {count++;};
    assert(Counter2_.prototype.constructor === Counter2_);
    var rv = {};
    rv.__proto__ = Counter2_.prototype;
    return rv;
};

var c = Counter2();

c.inc();
assert(c.get()===1);
assert(Object.getPrototypeOf(c)===Counter2.prototype);

var cb = Counter2();
assert(Object.getPrototypeOf(cb)===Counter2.prototype);
assert(cb.get()===0);
assert(c .get()===1, 'expecting c to be 1 but was:'+c.get());

. , , , Counter2 get , , count ( 0). , , Counter2 ( , ).

, Counter2 , count :

var Counter3 = function Counter3_() {
    var count = 0;
    var rv = {};
    rv.__proto__ = Counter3_.prototype;
    return rv;
};

Counter3.prototype.get = function() {return count;}; // this fails - I no longer have access to count lexical scope
Counter3.prototype.get = function() {return this.count;}; // this fails too

:

1) - , Counter2 ?

2) (.. "vanilla", new), / , ?

+4
1

1) Yep get inc . , "", .

function Counter2(name) {
    var count = 0;

    Counter2.prototype.get = function() {return count;};
    Counter2.prototype.inc = function() {count++;};

    var rv = {};
    if(name) {
        rv.get = function() {console.log(name); return count;};
    }
    console.log(count)
    rv.__proto__ = Counter2.prototype;
    return rv;
}

var a = Counter2('a');
var b = Counter2('b');
var c = Counter2();

a b get, get undefined . , count - , , , , , .

a.inc();a.inc();
[a.get(), b.get(), c.get()]

:

[0, 0, 2]

2) . , , , , . , new, .

, , a.inc() b , .

var count = 0;

function inc() {++count;}
function get() {return count;}

function Count() {
    return {inc: inc, get: get};
};

var a = Count();
var b = Count();

new, "" .

    function Count(){}
    Count.prototype.count = 0;
    Count.prototype.get = function() {return this.count;};
    Count.prototype.inc = function() {this.count++;};

var a = new Count();
var b = new Count();
a.inc();
a.get();
>>1
b.get();
>>0
a.inc === b.inc
>>true
0

Source: https://habr.com/ru/post/1614414/


All Articles