0

I thought javascript proxy can handle method calls, but this simple script fail to do so.

let s = new String("12345")
let p = new Proxy(s, {})
let x = p.startsWith("1") // type error here

Gives me, TypeError: String.prototype.toString requires that 'this' be a String. I think startsWith method were called with this=Proxy. How to properly redirect proxy's target to 'this' ?

  • Don't use proxies around native objects. – Bergi Dec 29 '21 at 19:46
  • …and don't use `String` objects in the first place, use plain primitive string values only. What is your goal? – Bergi Dec 29 '21 at 19:49
  • My goal is to check consistency of the underlying javascript semantics. It is very strange to see that some objects more "objects" than others. – synxroform Dec 29 '21 at 20:01
  • What does "*consistency of the underlying javascript semantics*" mean? If you want to evaluate an engine's conformance to javascript semantics, best run the [ECMAScript Test Suite](https://github.com/tc39/test262). – Bergi Dec 29 '21 at 20:05
  • I feel it's possible to use proxie around any object, just as in other languages. – synxroform Dec 30 '21 at 22:57

0 Answers0