I want to do something when creating a string, for example:
String = new Proxy(String, {
construct: (t, a) => {
return { a: 123 }
}
})
console.log(new String('q')) // { a: 123 }
However, if you use primitives, it doesn't work.
String = new Proxy(String, {
construct: (t, a) => {
return { a: 123 }
}
})
console.log('1') // Expected: { a: 123 }, Actual: 1
Is there any way?
then the second question is, when the runtime converts primitives, can I proxy the process?
var a = '123' // This is a primitive
console.log('123'.substring(0,1)) // Actual: 1
// The runtime wraps the primitive as a String object.
// then uses a substring, and then returns the primitive.
now:
String = new Proxy(String, {
construct: (t, a) => {
return { a: 123 }
},
apply: (target, object, args) => {
return { a: 123 }
}
})
console.log('1'.a) // Expected: 123 , Actual: undefined
I know I can add 'a' to the prototype of String to achieve the expectations.
But I want to be able to proxy access to arbitrary attributes for primitives.
(Is '1'.*
, Is not jsut '1'.a
)
Is there any way?
Thank you for your answer.