
你好,有人可以解釋一下const {r,g,b} = this;
為什么它沒有變數名,如果'this'是變數名,'this'指向什么?
function makeColor(r, g, b) {
const color = {};
color.r = r;
color.g = g;
color.b = b;
color.rgb = function () {
const { r, g, b } = this;
return `rgb(${r},${g},${b})`
}
}
uj5u.com熱心網友回復:
“this”這個詞不是一個變數。這是一個相對較新的概念。他們稱之為物件解構語法。
uj5u.com熱心網友回復:
“this”是方法的一個特殊屬性。當函式作為物件的方法被呼叫時,就像這里一樣,“this”指的是那個物件,在這種情況下是顏色物件,所以 color.rgb() 方法將回傳字串“rgb(r,g ,b)" 其中 r、g 和 b 是提供給用于創建該物件的 makeColor 函式的值。
uj5u.com熱心網友回復:
變數this甚至不存在,這是顛倒的詞之一(例如break,,,等),for在背景關系中您顯示指向常量,因此在背景關系中您顯示等于常量。functionletthiscolorthiscolor
更多關于thisMDN 的資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/470304.html
標籤:javascript 功能
