Constructor call for proxy related function

Suppose I have a function Foo, and I want the objects built from it to have the property bar:

function Foo() {}
Foo.prototype.bar = 'baz'
console.log('new Foo().bar: ' + new Foo().bar)

new Foo().bar: baz

Now suppose I bind Foo in some way. Related functions can still be used in constructor calls, and the border is thisignored:

const Bound = Foo.bind(42)
console.log('new Bound().bar: ' + new Bound().bar)

new Bound().bar: baz

Proxies must be shared and transparent. But...

const PFoo = new Proxy(Foo, { })
console.log('new PFoo().bar: ' + new PFoo().bar)

const PBound = new Proxy(Bound, { })
console.log('new PBound().bar: ' + new PBound().bar)

new PFoo().bar: baz
new PBound().bar: undefined

I would expect the second proxy to behave exactly like Boundsince I am using an empty handler. In other words, I expect the last exit to be baz.

Why is this not so?

(full fragment follows)

function Foo() {}
Foo.prototype.bar = 'baz'
console.log('new Foo().bar: ' + new Foo().bar)

const Bound = Foo.bind(42)
console.log('new Bound().bar: ' + new Bound().bar)

const PFoo = new Proxy(Foo, { })
console.log('new PFoo().bar: ' + new PFoo().bar)

const PBound = new Proxy(Bound, { })
console.log('new PBound().bar: ' + new PBound().bar)
Run codeHide result
+6
1

TL;DR

new F new.target F, F , new.target . .

, , . . :

: new.target.prototype. 5 .

: new F() new.target F (. ). ...

new Foo()

, new.target - Foo, Foo.prototype.

new Bound()

. new.target Bound. , 5 [[Construct]] : new.target , , Foo. , Foo.prototype .

new PFoo()

new.target PFoo, Foo, , PFoo.prototype Foo.prototype, .

new PBound()

new.target PBound. , [[Construct]] , new.target , , PBound.prototype, Bound.prototype. ...

function Foo() { }
Foo.prototype.iAm = 'Foo'
const Bound = Foo.bind(42)
Bound.prototype = {iAm: 'Bound'}
const Proxied = new Proxy(Bound, { })
console.log(new Proxied().iAm)
Hide result

: , new.target , , . , , - new.target - . , , , , .

+2

All Articles