Friday, 10 June 2022

How to extend the URL Class in url module of nodejs to add a property into it?

I am trying to extend the URL class and add a property to customize it. But its not working.

let { URL } = require('url');

class MyURL extends URL {
    constructor(url, base) {
        super(url, base);
        this.base = base;
    }
}
let ur = new MyURL('abc.html','http://www.example.com/about')
console.log(ur);

it logs

MyURL { href: 'http://www.example.com/abc.html', origin: 'http://www.example.com', protocol: 'http:', username: '', password: '', host: 'www.example.com', hostname: 'www.example.com', port: '', pathname: '/abc.html', search: '', searchParams: URLSearchParams {}, hash: '' }

Notice it doesn't have the base property. Why is it happening?

How can I make it have the property base with the base provided in the constructor by extending URL?



from How to extend the URL Class in url module of nodejs to add a property into it?

No comments:

Post a Comment