當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Node.js URL.username用法及代碼示例


URL.username 是 Node.JS 中 URL 類的內置應用程序編程接口 (API)。

URL.username API 用於獲取和設置 URL 的用戶名。

用法:

 url.username

URL:它是由 URL 構造函數創建的對象。

示例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);

輸出:

示例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);

輸出:


相關用法


注:本文由純淨天空篩選整理自AshishkrGoyal大神的英文原創作品 Node.js URL.username API。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。