JavaScript?中創(chuàng)建私有成員
前言:
面向?qū)ο缶幊陶Z言中的 private 關(guān)鍵字是一個訪問修飾符,可用于使屬性和方法只能在聲明的類中訪問。這使得隱藏底層邏輯變得容易,這些底層邏輯應(yīng)該被隱藏起來,并且不應(yīng)該與類的外部交互。
但是如何在 JavaScript 中實現(xiàn)類似的功能呢? 沒有保留關(guān)鍵字 private ,但在新的標(biāo)準(zhǔn)中 JavaScript 有自己的方法來創(chuàng)建類私有成員,但目前還處于 ES2020 試驗草案中,并且語法比較奇怪,以 # 作為前綴。下面介紹幾種在 JavaScript 代碼中實現(xiàn)私有屬性和方法的方式。
1.使用閉包
使用閉包可以使用私有屬性或者方法的封裝。利用閉包可以訪問外部函數(shù)的變量特征。
如下代碼片段:
function MyProfile() {
const myTitle = "DevPoint";
return {
getTitle: function () {
return myTitle;
},
};
}
const myProfile = MyProfile();
console.log(myProfile.getTitle()); // DevPoint
這可以轉(zhuǎn)化為將最頂層的自調(diào)用函數(shù)調(diào)用分配給一個變量,并且只用函數(shù)返回來公開它的一些內(nèi)部函數(shù):
const ButtonCreator = (function () {
const properties = {
width: 100,
height: 50,
};
const getWidth = () => properties.width;
const getHeight = () => properties.height;
const setWidth = (width) => (properties.width = width);
const setHeight = (height) => (properties.height = height);
return function (width, height) {
properties.width = width;
properties.height = height;
return {
getWidth,
getHeight,
setWidth,
setHeight,
};
};
})();
const button = new ButtonCreator(600, 360);
console.log(button.getHeight()); // 360
2.使用 ES6 類
為了使代碼更類似于 OOP 方法,可以使用 ES6 中引入的 class 關(guān)鍵字。要使屬性和方法私有,可以在類之外定義它們。
就對上面的 ButtonCreator 的例子使用 class 進行重構(gòu):
const properties = {
width: 100,
height: 50,
};
class ButtonCreator {
constructor(width, height) {
properties.width = width;
properties.height = height;
}
getWidth = () => properties.width;
getHeight = () => properties.height;
setWidth = (width) => (properties.width = width);
setHeight = (height) => (properties.height = height);
}
const button = new ButtonCreator(600, 360);
console.log(button.getHeight()); // 360
現(xiàn)在假設(shè)屬性是公共的,但想在私有方法中使用它們,其中上下文指向 ButtonCreator,可以通過以下方式實現(xiàn)它:
const privates = {
calculateWidth() {
return this.width;
},
};
class ButtonCreator {
constructor(width, height) {
this.width = width;
this.height = height;
}
getWidth = () => privates.calculateWidth.call(this);
getHeight = () => this.height;
setWidth = (width) => (this.width = width);
setHeight = (height) => (this.height = height);
}
const button = new ButtonCreator(600, 360);
console.log(button.getHeight()); // 360
上面的代碼使用了 Function.prototype.call,它用于調(diào)用具有給定上下文的函數(shù)。在例子中,使用 ButtonCreator 類的上下文。
如果私有函數(shù)也需要參數(shù),可以將它們作為附加參數(shù)傳遞以調(diào)用:
const privates = {
calculateWidth(percent) {
return this.width * percent;
},
};
class ButtonCreator {
constructor(width, height) {
this.width = width;
this.height = height;
}
getWidth = () => privates.calculateWidth.call(this, 0.1);
getHeight = () => this.height;
setWidth = (width) => (this.width = width);
setHeight = (height) => (this.height = height);
}
const button = new ButtonCreator(600, 360);
console.log(button.getWidth()); // 60
3.使用 ES2020 提案
還處于 ES2020 試驗草案中,引入了私有方法或者屬性的定義,語法比較奇怪,以 # 作為前綴。
class ButtonCreator {
#width;
#height;
constructor(width, height) {
this.#width = width;
this.#height = height;
}
// 私有方法
#calculateWidth() {
return this.#width;
}
getWidth = () => this.#calculateWidth();
getHeight = () => this.#height;
setWidth = (width) => (this.#width = width);
setHeight = (height) => (this.#height = height);
}
const button = new ButtonCreator(600, 360);
console.log(button.width); // undefined
console.log(button.getWidth()); // 600
4.使用 WeakMap
這種方法建立在閉包方法之上,使用作用域變量方法創(chuàng)建一個私有 WeakMap,然后使用該 WeakMap 檢索與此相關(guān)的私有數(shù)據(jù)。這比作用域變量方法更快,因為所有實例都可以共享一個 WeakMap,所以不需要每次創(chuàng)建實例時都重新創(chuàng)建方法。
const ButtonCreator = (function () {
const privateProps = new WeakMap();
class ButtonCreator {
constructor(width, height, name) {
this.name = name; // 公共屬性
privateProps.set(this, {
width, // 私有屬性
height, // 私有屬性
calculateWidth: () => privateProps.get(this).width, // 私有方法
});
}
getWidth = () => privateProps.get(this).calculateWidth();
getHeight = () => privateProps.get(this).height;
}
return ButtonCreator;
})();
const button = new ButtonCreator(600, 360);
console.log(button.width); // undefined
console.log(button.getWidth()); // 600
這種方式對于私有方法的使用有點別扭。
5.使用 TypeScript
可以將 TypeScript 用作 JavaScript 的一種風(fēng)格,可以使用 private 關(guān)鍵字從面向?qū)ο蟮恼Z言中真正重新創(chuàng)建功能。
class ButtonCreator {
private width: number;
private height: number;
constructor(width: number, height: number) {
this.width = width;
this.height = height;
}
private calculateWidth() {
return this.width;
}
public getWidth() {
return this.calculateWidth();
}
public getHeight() {
return this.height;
}
}
const button = new ButtonCreator(600, 360);
console.log(button.getWidth()); // 600
console.log(button.width); // error TS2341: Property 'width' is private and only accessible within class 'ButtonCreator'.
總結(jié):
到此這篇關(guān)于JavaScript 中創(chuàng)建私有成員的文章就介紹到這了,更多相關(guān)JavaScript 中創(chuàng)建私有成員內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript 刪除select中的所有option的實例
這篇文章主要介紹了javascript 刪除select中的所有option的實例的相關(guān)資料,希望通過本文能幫助到大家實現(xiàn)這樣的功能,需要的朋友可以參考下2017-09-09
JavaScript遍歷查找數(shù)組中最大值與最小值的方法示例
這篇文章主要介紹了JavaScript遍歷查找數(shù)組中最大值與最小值的方法,結(jié)合實例形式分析了javascript基于數(shù)組遍歷、判斷實現(xiàn)最大值與最小值計算的相關(guān)操作技巧,需要的朋友可以參考下2019-05-05
javascript實現(xiàn)的字符串與十六進制表示字符串相互轉(zhuǎn)換方法
這篇文章主要介紹了javascript實現(xiàn)的字符串與十六進制表示字符串相互轉(zhuǎn)換方法,涉及javascript字符串轉(zhuǎn)換的相關(guān)技巧,在防止SQL注入和XSS中具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07

