📜  节点 | URL.用户名API

📅  最后修改于: 2022-05-13 01:56:57.378000             🧑  作者: Mango

节点 | URL.用户名API

URL.usernameNode.js的内URL类的一个内置的应用程序编程接口(API)。
URL.username API 用于获取和设置 URL 的用户名。

Syntax: url.username
url : It is an object created by URL constructor.

示例1:(获取URL的用户名)

javascript
//Creating an URL_1 object with URL constructor.
const URL_1 = new URL("https://ashish:ashish123@www.geeksforgeeks.org");
 
//Getting username of above created URL_1 object
console.log(URL_1.username);


javascript
//Creating an URL_1 object with URL constructor.
const URL_1 = new URL("https://ashish:ashish123@www.geeksforgeeks.org/geeks");
 
//Getting username of above created URL_1 object
console.log("Before changing username URL is:")
console.log(URL_1.href);
console.log("username: "+ URL_1.username);
 
//Setting URL_1 username to ashu
URL_1.username = "ashu";
 
//Getting username after setting it to ashu
console.log("After changing username URL is:")
console.log(URL_1.href);
console.log("username: "+ URL_1.username);


输出:



例2:(设置URL的用户名)

javascript

//Creating an URL_1 object with URL constructor.
const URL_1 = new URL("https://ashish:ashish123@www.geeksforgeeks.org/geeks");
 
//Getting username of above created URL_1 object
console.log("Before changing username URL is:")
console.log(URL_1.href);
console.log("username: "+ URL_1.username);
 
//Setting URL_1 username to ashu
URL_1.username = "ashu";
 
//Getting username after setting it to ashu
console.log("After changing username URL is:")
console.log(URL_1.href);
console.log("username: "+ URL_1.username);

输出: